JesseToxik Posted June 2, 2013 Share Posted June 2, 2013 <?php$filename = "hits.txt";if (!file_exists($filename)){// hits.txt doesn't exist, let's try to create it.$fd = fopen($filename, "w+");fclose($fd);}$file = file($filename);$file = array_unique($file);$hits = count($file);// Print out the number of unique visitors we have had.echo $hits;$fd = fopen($filename, "r");$fstring = fread($fd, filesize($filename));fclose($fd);$fd = fopen($filename, "w");$fcounted = $fstring . "" . $_SERVER["REMOTE_ADDR"];$fout = fwrite($fd, $fcounted);fclose($fd);?> am using a hit counter script. It records ip address in a text file. Problem is it records the same ip over and over. How can I make it record the ip one time and one time only? Quote Link to comment Share on other sites More sharing options...
JesseToxik Posted June 2, 2013 Author Share Posted June 2, 2013 Sorry for the messed up post. Made this on my android. Quote Link to comment Share on other sites More sharing options...
dannon Posted June 2, 2013 Share Posted June 2, 2013 You could store the IPs in database and make the IP column unique. Quote Link to comment Share on other sites More sharing options...
JesseToxik Posted June 2, 2013 Author Share Posted June 2, 2013 I'm trying to avoid data bases as I am new to them and. Quote Link to comment Share on other sites More sharing options...
exze Posted June 2, 2013 Share Posted June 2, 2013 Maybe not post on Android? So your posts are clearer? Anyhow, there are 2 options, as above use a database, the best and easiest way. Or you could store the IP on a new line in a text file, everytime someone visits your site loop through the file and check if the IP is in there if it is don't add it, else do add it, then just count the lines of the text file. Quote Link to comment Share on other sites More sharing options...
cpd Posted June 2, 2013 Share Posted June 2, 2013 Scan the text file for the IP address, if you find it do nothing, if you don't add it to the text file. Quote Link to comment Share on other sites More sharing options...
JesseToxik Posted June 3, 2013 Author Share Posted June 3, 2013 Cpd I'm not sure how to do that. Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 3, 2013 Share Posted June 3, 2013 Did you bother to google "PHP read contents of file" or anything? Quote Link to comment Share on other sites More sharing options...
cpd Posted June 3, 2013 Share Posted June 3, 2013 Do you want a bib and high chair? Quote Link to comment Share on other sites More sharing options...
DaveyK Posted June 3, 2013 Share Posted June 3, 2013 And cushion for comfort... There are plenty of tutorials on this... You can either store it in a database (which would make the most sense) or store it in a file. If you store it in a file, you could serialize or JSON encode an array and then decode it for full functionality. You can also store each entry on a new line. Whichever suits your need. Get creative, search the webs. Dont ask us to do your stuff for you. Quote Link to comment Share on other sites More sharing options...
JesseToxik Posted June 3, 2013 Author Share Posted June 3, 2013 The code I posted above works I am just unsure as to how to loop and check. I am not asking it to be done for me. Simply guidance on making that code better. Quote Link to comment Share on other sites More sharing options...
cpd Posted June 3, 2013 Share Posted June 3, 2013 http://php.net/file RTFM Quote Link to comment Share on other sites More sharing options...
DaveyK Posted June 3, 2013 Share Posted June 3, 2013 You should parse code in between the code tags, identified by the "<>" icon. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.