PHP计数器使用文件


PHP counter using files

我找到了一个非常创新和体面的计数器,它让我把计数器代码放在主index.php页面上,然后通过我的后端系统查看计数器,然而,尽管它可以工作,但由于PHP错误,由于旧代码,它会破坏主页面;我知道一些PHP,但不足以知道我要修复什么。

counter tutorial: counter tutorial link

count.db

0%0%0%0000 00 00%0

counter.php:

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$file_ip = fopen('counter/ip.db', 'rb');
while (!feof($file_ip)) $line[]=fgets($file_ip,1024);
for ($i=0; $i<(count($line)); $i++) {
    list($ip_x) = split("'n",$line[$i]);
    if ($ip == $ip_x) {$found = 1;}
}
fclose($file_ip);
if (!($found==1)) {
    $file_ip2 = fopen('counter/ip.db', 'ab');
    $line = "$ip'n";
    fwrite($file_ip2, $line, strlen($line));
    $file_count = fopen('counter/count.db', 'rb');
    $data = '';
    while (!feof($file_count)) $data .= fread($file_count, 4096);
    fclose($file_count);
    list($today, $yesterday, $total, $date, $days) = split("%", $data);
    if ($date == date("Y m d")) $today++;
        else {
            $yesterday = $today;
            $today = 1;
            $days++;
            $date = date("Y m d");
        }
    $total++;
    $line = "$today%$yesterday%$total%$date%$days";
    $file_count2 = fopen('counter/count.db', 'wb');
    fwrite($file_count2, $line, strlen($line));
    fclose($file_count2);
    fclose($file_ip2);
}
?>

showcounter.php

<table>
<tr>
<th colspan="2">Unique visitors</th>
</tr>
<tr>
<td><b>Today</b></td>
<td>
<?php
$file_count = fopen('counter/count.db', 'rb');
$data = '';
while (!feof($file_count)) $data .= fread($file_count, 4096);
fclose($file_count);
list($today, $yesterday, $total, $date, $days) = split("%", $data);
echo $today;
?>
</td>
</tr>
<tr>
<td><b>Yesterday</b></td>
<td>
<?php
echo $yesterday;
?>
</td>
</tr>
<tr>
<td><b>Total</b></td>
<td>
<?php
echo $total;
?>
</td>
</tr>
<tr>
<td><b>Daily average</b></td>
<td>
<?php
echo ceil($total/$days);
?>
</td>
</tr>
</table>

感谢所有的回复,非常感谢,希望我们能让它再次工作:)

编辑:刚刚崩溃了我的浏览器给你们一些错误信息:p

Warning: fopen(ip.db) [function.fopen]: failed to open stream: No such file or directory in /counter/counter.php on line 4
Warning: feof(): supplied argument is not a valid stream resource in /counter/counter.php on line 5
Warning: fgets(): supplied argument is not a valid stream resource in /counter/counter.php on line 5
Warning: feof(): supplied argument is not a valid stream resource in /counter/counter.php on line 5

显示"no such file"但是ip.db上传到了/counter/

Contents of the /counter/ folder:
count.db
counter.php
ip.db
index.html
showcounter.php

您说您的文件命名为countdb.php,但在代码中没有对该文件名的单个引用。确保你的文件命名正确