PHP counter using files
I found a really innovative and decent counter which lets me put the counter code on the main index.php page and then view the counter through my back end system, however although it kind of works it breaks the main page because of PHP errors, becasue of the old code; I know some PHP but not enough to know what im looking to fix.
counter tutorial: counter tutorial link
count.db
0%0%0%0000 00 00%0
counter.php:
<?php
$ip = $_SE开发者_运维百科RVER['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>
Thanks for all replies, much appreciated, and hopefully we can get this working again:)
EDIT: just crashed my browser to get you guys some error messages :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
It does say, "no such file" but ip.db IS uploaded to /counter/
Contents of the /counter/ folder:
count.db
counter.php
ip.db
index.html
showcounter.php
ip and count.db, chmod'd to 666
You say your file is named countdb.php
, but there is not one single reference to that filename in the code. Make sure you have your files named correctly.
精彩评论