
July 15th, 2002, 03:49 PM
|
|
Registered User
|
|
Join Date: Jun 2002
Location: UK
Posts: 15
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Need help improving daily visitors counter script
Hi Guys
I wrote this simple daily visitors counter script yesterday for a tutorial that I'm writing for my site demonstrating file handling in PHP. But am currently to embarrassed by my messy inefficient code it to post it. I could really do with a hand in improving it. I am sure that there are much more efficient ways to do what I've done.
Any ideas as to how I can make it better???
Retr0
Call the script and counter.txt and script file via includes.
Code:
<?php include("counter.txt"); ?>
<IMG border=0 width=1 height=1 src="daily_visitors/daily_visitors.php" >
<?php
// Daily visitors counter script
// Holds the current day of year as a numerical value 1 - 365
$day = $PATH_INFO."day.txt";
// Are counter value txt file
$counter = $PATH_INFO."counter.txt";
// Are IP address value txt file
$ip = $PATH_INFO."ip.txt";
// Number of previous IP address values to keep in txt file
$ip_block = 200;
$fp = fopen("$ip", "a");
if (IsSet($REMOTE_ADDR))
fwrite($fp, "$REMOTE_ADDR\n");
else
{
$REMOTE_ADDR = 0;
} // end else
fclose($fp);
$fp = fopen($ip, "r");
$fr = fread($fp, filesize($ip));
// explode $REMOTE_ADDR and place in a array
$line = explode("$REMOTE_ADDR", $fr);
// $i == $REMOTE_ADDR +1 for are \n
for($i = 0; $i < count($line); $i++);
fclose($fp);
// Line numbers
$fcontents = file ("$ip");
while (list ($line_num, $line) = each ($fcontents))
{
if ($line_num > $ip_block)
{
$fp = fopen("$ip", "w");
fwrite($fp, "");
fclose($fp);
} // end if
} // end while
if ($i > 2)
{
// Are IP address has been here recently
exit;
}
else
{
// Open are day of year file
$fd = fopen("$day", "r");
$old = fgets($fd, 1024);
fclose($fd);
// If the day of year file is empty
// write day of year as a numerical value 1 - 365
If ($old == "")
{
$r = date("z");
$fs = fopen($day, "w");
fwrite($fs, "$r");
fclose ($fs);
} // end if
// Increment 'z' day of year number
If ($old == date("z"))
{
$o = 1 + date("z");
$fs = fopen($day, "w");
fwrite($fs, $o);
fclose ($fs);
// Reset are counter
$fr = fopen("$counter", "w");
fwrite($fr, "1");
fclose($fr);
} // end if
else
{
// Read Counter
$fp = fopen("$counter", "r");
$count = fgets($fp, 1024);
fclose($fp);
// Current counter file empty so writing a 1
If ($count == "")
{
$fr = fopen("$counter", "w");
fwrite($fr, "1");
fclose($fr);
} // end if
// Increment counter number
$fw = fopen("$counter", "w");
$output = $count++;
$countnew = fputs($fw, $count);
fclose($fw);
} // end else
} // end else
?>
|