使用php统计网站访问者
我用一个基本的脚本计算我网站的访问者:使用php统计网站访问者,php,Php,我用一个基本的脚本计算我网站的访问者: <?php if(!isset($_COOKIE['countedUser'])) { setcookie("countedUser", "0", time() + (86400 * 30)); $countfile = fopen("counterfile.txt", "w+") or die("Unable to open file!"); $new = intval(fgets($countfile)) + 1;
<?php
if(!isset($_COOKIE['countedUser'])) {
setcookie("countedUser", "0", time() + (86400 * 30));
$countfile = fopen("counterfile.txt", "w+") or die("Unable to open file!");
$new = intval(fgets($countfile)) + 1;
fwrite($countfile, $new);
fclose($countfile);
}
echo file_get_contents('counterfile.txt');
反文件包含数字0
这适用于从0到1,但在这之后就不起作用了。最可能的原因是fgets$countfile总是返回NULL。 既然intvalNULL=0
因此,$new++永远不会超过1。当cookie不存在时,您已经初始化并递增1。但是,当它存在时,您没有递增
if(!isset($_COOKIE['countedUser']))
{
//initialize cookie
}
else
{
// increment current value
}
我想你应该知道cookie是什么
…从网站发送并存储在用户计算机上的一小段数据
当用户浏览时,由用户的web浏览器对计算机进行访问
每次打开counterfile.txt时都会截断它:
w+:开放阅读和写作;将文件指针放在文件的开头,并将文件截断为零长度。如果文件不存在,请尝试创建它
请注意,您可能希望将手柄聚集在一起,以防止出现竞速情况。LOCK\u EX=独占锁,LOCK\u SH=共享锁