使用文件的PHP计数器

使用文件的PHP计数器,php,syntax,counter,Php,Syntax,Counter,我发现了一个非常创新和体面的计数器,它允许我将计数器代码放在main index.php页面上,然后通过我的后端系统查看计数器,然而,尽管它可以正常工作,但由于旧代码的缘故,它会因php错误而破坏主页;我知道一些PHP,但不足以知道我要修复什么 反教程: count.db 0%0%0%0000 00 00%0 counter.php: <?php $ip = $_SERVER['REMOTE_ADDR']; $file_ip = fopen('counter/ip.db', 'rb')

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

反教程:

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);
}
?>
它确实说“没有这样的文件”,但ip.db被上传到/counter/

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

ip和count.db,chmod'd到666

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

对不起,在顶部应该是count.db而不是countdb.php,并将其放在带有错误的代码之前?(注释掉代码的其余部分以避免崩溃)
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
Contents of the /counter/ folder:
count.db
counter.php
ip.db
index.html
showcounter.php