当我将其移动到另一台服务器时,计数器将无法工作

发布于 2024-07-25 02:55:51 字数 1694 浏览 8 评论 0原文

我设置了一个点击计数器,以便每次有人访问我的网站时,都会加载 7 个不同侧边栏之一。 有一个名为 counter.php 的文件写入名为 hitcounter.txt 的文本文件。 所有对文件的引用似乎都是相对的,但是当我将它们移动到新主机上的新目录时,我得到了这个错误,而不是一个愉快的点击计数器:

警告:fopen(hitcounter.txt) [function.fopen]:无法打开流:/usr/local/apache/sites 中的权限被拒绝/MY-SITE/counter.php 上线 5

Counter.php完整粘贴在下面,第5行是第一个引用fopen,counter.php和hitcounter.txt都有775权限,与旧主机上的操作相同。

我究竟做错了什么? 我显然错过了一些非常简单和令人尴尬的事情,所以在帮助我的同时,请随意给予我任何蔑视或辱骂。

计数器.php:

<?php
    $count_my_page = ("hitcounter.txt");
    $hits = file($count_my_page);
    $hits[0]++;
    $fp = fopen($count_my_page , "w");
    if ($fp) {
        fputs($fp , "$hits[0]");
        fclose($fp);
    }
    if($hits[0]<=1)
        $random_number=0;
    else if($hits[0]>1 && $hits[0]<=2)
        $random_number=1;
    else if($hits[0]>2 && $hits[0]<=3)
        $random_number=2;
    else if($hits[0]>3 && $hits[0]<=4)
        $random_number=3;
    else if($hits[0]>4 && $hits[0]<=5)
        $random_number=4;
    else if($hits[0]>5 && $hits[0]<=6)
        $random_number=5;
    else if($hits[0]>6 && $hits[0]<=7)
        $random_number=6;
    else if($hits[0]>7 && $hits[0]<=8)
        $random_number=7;
    else if($hits[0]>8 && $hits[0]<=9) {
        $random_number=8;
        if($hits[0]==9) {
            $count_my_page=("hitcounter.txt");
            $fp = fopen($count_my_page , "w");
            $hits[0]=0;
            fputs($fp , "$hits[0]");
            fclose($fp);
        }
    }
?>

I set up a hitcounter so that each time someone goes to my site, one of 7 different sidebars loads up. There is a file called counter.php that writes to a text file called hitcounter.txt. All the references to files seem to be relative but when I moved them to a new directory at my new host I got this error instead of a happy hit counter:

Warning: fopen(hitcounter.txt) [function.fopen]: failed to open stream: Permission denied in /usr/local/apache/sites/MY-SITE/counter.php on line 5

Counter.php is pasted in its entirety below, line 5 is the first reference to fopen, both counter.php and hitcounter.txt have 775 permissions, same as they did on the old host.

What am I doing wrong? I'm obviously missing something really simple and embarrassing, so feel free to give me any scorn or abuse with while helping me out.

counter.php:

<?php
    $count_my_page = ("hitcounter.txt");
    $hits = file($count_my_page);
    $hits[0]++;
    $fp = fopen($count_my_page , "w");
    if ($fp) {
        fputs($fp , "$hits[0]");
        fclose($fp);
    }
    if($hits[0]<=1)
        $random_number=0;
    else if($hits[0]>1 && $hits[0]<=2)
        $random_number=1;
    else if($hits[0]>2 && $hits[0]<=3)
        $random_number=2;
    else if($hits[0]>3 && $hits[0]<=4)
        $random_number=3;
    else if($hits[0]>4 && $hits[0]<=5)
        $random_number=4;
    else if($hits[0]>5 && $hits[0]<=6)
        $random_number=5;
    else if($hits[0]>6 && $hits[0]<=7)
        $random_number=6;
    else if($hits[0]>7 && $hits[0]<=8)
        $random_number=7;
    else if($hits[0]>8 && $hits[0]<=9) {
        $random_number=8;
        if($hits[0]==9) {
            $count_my_page=("hitcounter.txt");
            $fp = fopen($count_my_page , "w");
            $hits[0]=0;
            fputs($fp , "$hits[0]");
            fclose($fp);
        }
    }
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浸婚纱 2024-08-01 02:55:52

应该和权限有关系。 将其设置为 777 看看会发生什么。 如果 apache 以自己的权限运行并且不属于您的一部分,这可能是原因,但我有一些建议:

  • 使用 file_put_contents/file_ get_contents 进行简单的读/写!
  • 如果可能,请使用 $random_number = rand(0,8) 或 mt_rand(0,8) 而不是这些无数行,并作为奖励摆脱所有文件读/写

祝你好运!

更新

没有什么比一个很好的例子更好的了:

<?php 
  $random_number = mt_rand(0,8);
  file_put_contents("hitcounter.txt", $random_number); /* dont know if you still need it */
?>

如果你真的想要(顺便说一句,不是随机的!):

<?php 
  $file = "hitcounter.txt";
  $number = (int)file_get_contents($file);
  $number = ++$number % 9;
  file_put_contents($file, $number);
?>

It probably has something to do with permissions. Set it to 777 and see what happens. If apache runs with own permissions and is not part of your this might be the reason, but I have a couple of suggestions:

  • use file_put_contents/file_ get_ contents for simple read/writes!
  • please use $random_number = rand(0,8) OR mt_rand(0,8) if possible instead of these countless lines and as a bonus get rid of all the file reading/writing

Good luck!

Update

Nothing beats a nice example:

<?php 
  $random_number = mt_rand(0,8);
  file_put_contents("hitcounter.txt", $random_number); /* dont know if you still need it */
?>

If you really want to (btw. NOT random!):

<?php 
  $file = "hitcounter.txt";
  $number = (int)file_get_contents($file);
  $number = ++$number % 9;
  file_put_contents($file, $number);
?>
画骨成沙 2024-08-01 02:55:52

首先,你不需要775权限。 您需要对 hitcounter.txt 拥有 666 权限。 PHP 文件可以是 644。Web

服务器可能不是该组的成员,具体取决于主机,因此您需要授予“Everyone”组写入权限。

文件夹需要“执行”位,但单个文件不需要,因为它们不由操作系统执行。

所以你知道,775 是:所有者、组、每个人

  • 所有者 = 读 + 写 + 执行

  • 组 = 读 + 写 + 执行

  • 每个人 = 读取 + 执行

666 表示

  • 所有者 = 读取 + 写入

  • 组 = 读取 + 写入< /p>

  • 每个人 = 读 + 写

644 表示

  • 所有者 = 读 + 写

  • 组 = 读

  • 每个人 = 阅读

First, you don't need 775 permissions. You need 666 permissions on the hitcounter.txt. The PHP file can be 644.

The web server probably isn't a member of the group, depending on the host, so you'd need to give the 'Everyone' group write permissions.

The "Execute" bit is needed for folders but not for individual files, since they are not being executed by the OS.

So you know, the 775 is : Owner, Group, Everyone

  • Owner = Read + Write + Execute

  • Group = Read + Write + Execute

  • Everyone = Read + Execute

666 means

  • Owner = Read + Write

  • Group = Read + Write

  • Everyone = Read + Write

644 means

  • Owner = Read + Write

  • Group = Read

  • Everyone = Read

蔚蓝源自深海 2024-08-01 02:55:52

尝试将权限设置为 777。如果这不起作用,可能是您的主机不允许在服务器上进行文件操作

Try setting permissions to 777. If that doesnt work, could be your host doesnt allow file manipulation on the server

巴黎盛开的樱花 2024-08-01 02:55:52

检查您的 include_path 以确保其中包含句点(当前目录)。 不知道为什么不会,但你永远不知道你的主人是否有问题。 您可以使用 ini_get 从 PHP 脚本打印出您的 include 路径

ini_get('include_path');

:除此之外,您可能需要不同的文件权限。 这是一个有用的网站,描述了有关文件权限的大量信息。

Check your include_path to make sure it has a period (current directory) in it. Not sure why it wouldn't, but you never know if your host is wonkey. You can print out your include path from a PHP script using ini_get:

ini_get('include_path');

Other than that, you probably need different file permissions. Here's a useful site describing a lot of information on file permissions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文