当我将其移动到另一台服务器时,计数器将无法工作
我设置了一个点击计数器,以便每次有人访问我的网站时,都会加载 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
应该和权限有关系。 将其设置为 777 看看会发生什么。 如果 apache 以自己的权限运行并且不属于您的一部分,这可能是原因,但我有一些建议:
祝你好运!
更新
没有什么比一个很好的例子更好的了:
如果你真的想要(顺便说一句,不是随机的!):
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:
Good luck!
Update
Nothing beats a nice example:
If you really want to (btw. NOT random!):
首先,你不需要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
尝试将权限设置为 777。如果这不起作用,可能是您的主机不允许在服务器上进行文件操作
Try setting permissions to 777. If that doesnt work, could be your host doesnt allow file manipulation on the server
检查您的
include_path
以确保其中包含句点(当前目录)。 不知道为什么不会,但你永远不知道你的主人是否有问题。 您可以使用 ini_get 从 PHP 脚本打印出您的include 路径
:除此之外,您可能需要不同的文件权限。 这是一个有用的网站,描述了有关文件权限的大量信息。
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 yourinclude path
from a PHP script using ini_get:Other than that, you probably need different file permissions. Here's a useful site describing a lot of information on file permissions.