Hit Counter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davy2001
Forum Commoner
Posts: 37
Joined: Tue Feb 24, 2004 5:59 pm

Hit Counter

Post by davy2001 »

Hi,
i have made a hit counter in flash and it uses php to work. i got everything to work but now im trying to make it better so that it logs the users visited and i a trying to make it so that it doesnt log my ip address and and a few more addresses. here is my code so far:

Code: Select all

<?php

$count = file_get_contents("count.txt");

$count = explode("=", $count);
$count[1] = $count[1]+1;

$ipad1 = 127.0.0.0;
$ipad2 = 192.168.1.1;
$ipad3 = localhost;

$ip = $_SERVER['REMOTE_ADDR'];
$date = date("n-j-Y, g:i:sa");
if ($ip == "$ipad[]"){
echo "";
} else {
$file = fopen("count.txt", "w+");
fwrite($file, "count=".$count[1]);
fclose($file);

$filename = 'logs/weblog.html';
$fp = fopen($filename, "a");
$string = "
<tr>
<td bgcolor=white><font face=arial size=1 color=black>
<center>$date</td>
<td bgcolor=white><font face=arial size=1 color=black>
<center>$ip</td>
</tr>";
$write = fputs($fp, $string);
fclose($fp);

print "count=".$count[1];
}
?>

it should save the log to weblog.html.

Dave
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Errors? What's not working?
davy2001
Forum Commoner
Posts: 37
Joined: Tue Feb 24, 2004 5:59 pm

Post by davy2001 »

i tested it on a dialup machine and it didnt change the number in the text file :s

i want it to allow other users appart from the ip addresses in the code to work the counteer. this is bcos im always viewing my website and updating it. all the hits will be me.

Dave
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Not too sure but try that

Code: Select all

<?php


$count = file_get_contents("count.txt"); 

$count = explode("=", $count); 
$count[1] = $count[1]+1; 

$ips = array("127.0.0.0","192.168.1.1","localhost");
$ip = $_SERVER['REMOTE_ADDR']; 
$date = date("n-j-Y, g:i:sa"); 
if ($ip != "$ips[]"){  
$file = fopen("count.txt", "w+"); 
fwrite($file, "count=".$count[1]); 
fclose($file); 

$filename = 'logs/weblog.html'; 
$fp = fopen($filename, "a"); 
$string = " 
<tr> 
<td bgcolor=white><font face=arial size=1 color=black> 
<center>$date</td> 
<td bgcolor=white><font face=arial size=1 color=black> 
<center>$ip</td> 
</tr>"; 
$write = fputs($fp, $string); 
fclose($fp); 

print "count=".$count[1]; 
} 
 

?>
Post Reply