cool, so i thought i would make an improvement so that the hits.txt file doesnt get super long and take forever to parse:
also note that i fixed the error in the code that caused a warning to display when you upload a blank hits.txt text file and run the code for the first time.
i also solved the issue some people were having (me included) of having duplicate hits register if you refresh the page.
hope you enjoy the update!
p.s. if it's your first time loading the page, then it displays a welcome message (you may want to remove this)
<?php
/* be sure to change the permissions of hits.txt to 777
using either chmod or filezilla
*/
$filename = "hits.txt";
$fileIn = file($filename);
$file = array_unique($fileIn); //convert to an array with only unique items in it
$hits = count($file); //count how many unique lines are in the file
echo $hits;
$fd = fopen ($filename , "r"); //open file to read
if ($hits > 0){
$fstring = fread ($fd , filesize ($filename)); //read in as string
}
fclose($fd);
$myAddr = getenv("REMOTE_ADDR"); //get the ip address of client
/*check to see if they've already logged an entry, if so,
then don't log another one in order to save on the file size
*/
$pos = strpos($fstring, $myAddr);
if ($pos !== false) {
}
else{
$fd = fopen ($filename , "w");
$fcounted = $fstring."\n".$myAddr;
$fout= fwrite ($fd , $fcounted );
fclose($fd);
echo " welcome, new visitor!";
}
?>