hello friends, I am trying to make a simple unique hit counter with ip address and .txt file but somehow this code is not working ..me using wamp server to test the code this is my count.php <?php function hit_count(){ $ip_address =$_SERVER["REMOTE_ADDR"]; $ip_file = file("ip.txt"); foreach($ip_file as $ip){ $ip_single = trim($ip); if($ip_address == $ip_single){ $found = true; break; }else{ $found = false; } } if($found==false){ $filename="count.txt"; $handle = fopen($filname,"r"); $current =fread($handle,filesize($filename)); //echo $current; fclose($handle); $current_inc = $current+1; $handle = fopen($filname,"w"); fwrite($handle,$current_inc); fclose($handle); $handle = fopen("ip.txt","a"); fwrite($handle,$ip_address . "\n"); fclose($handle); } } ?> PHP: and this is my index.php <?php //hit counter include "count.php"; hit_count(); ?> PHP: i have ip.txt(this is blank) file and count.txt(value set to 0) file on the server but when i refresh page ..counter is not counting page hits.. any ideas?
We'll start with the niggly stuff use filter_input(INPUT_SERVER, 'REMOTE_ADDR') to get the ip address on line 19 you have a typo - referring to $filename as $filname $filename="count.txt"; $handle = fopen($filname,"r"); You file handling seems off, too much opening and unnecessary closing Then there's the content of count.txt. I'm not sure what you think you're recording - literally just the number of hits?
thanks but it still doesn't work here is the modified code <?php function hit_count(){ $ip_address =filter_input(INPUT_SERVER, "REMOTE_ADDR"); $ip_file = file("ip.txt"); foreach($ip_file as $ip){ $ip_single = trim($ip); if($ip_address == $ip_single){ $found = true; break; }else{ $found = false; } } if($found==false){ $filename="count.txt"; $handle = fopen($filename,"r"); $current =fread($handle,filesize($filename)); //echo $current; fclose($handle); $current_inc = $current+1; $handle = fopen($filname,"w"); fwrite($handle,$current_inc); fclose($handle); $handle = fopen("ip.txt","a"); fwrite($handle,$ip_address . "\n"); fclose($handle); } } ?> PHP: me still learning php..so this is is just tutorial stuff
A big part of programming is knowing how to debug. Chuck some var_export() commands in there to see what variables have. Let us know which lines aren't doing what you expect and describe your expectation versus what your code is doing.
1) why not just have the true condition RETURN? Would skip a bunch of excess junk. Why are you opening and closing files willy-nilly and reading the entire file? Why read the entire file if you might be able to short-circuit out prematurely? <?php function hitCount(){ $ipAddress =$_SERVER["REMOTE_ADDR"]; $handle = fopen('ip.txt', 'r+'); while ($line = fgets($handle)) { if ($line == $ipAddress) { fclose($handle); return; } } fputs($ipAddress . "\r\n"); fclose($handle); $handle = fopen('count.txt', 'r+'); $current = fread($handle); fseek($handle, 0); fwrite($handle, $current++); fclose($handle); } Code (markup): Untested, might have typo's, but is likely what you're trying to do in a more compact and efficient form.
1) Issue with line 21 - $current =fread($handle,filesize($filename)); You are trying to read an empty file therefore it fails. I would combine lines 21,22 and 23 into $current = file_get_contents($filename); 2) Typo in line 27 - $handle = fopen($filname,"w"); should be $handle = fopen($filename,"w"); Once I made the above changes it worked for me.
Please don't store value in files. You can use database because files are slower and database is faster. Files are not safe to store IP Address of users/visitors
I agree it's best practice to use a database. I wouldn't consider IP address as sensitive information though. A person's IP can be seen in any server log... For this purpose flat file would work just fine as long as it doesn't become too large. A database seems like a bit Overkill
@RahulSaini Why is storing IP in a file unsafe? IP adress is nothing sensitive. Its not a password. There are 2 problems with that code. 1. Your count.txt file is empty in the beginning, so this line is not doing anything: $current_inc = $current+1; Put a number in the count.txt file to start with, and this will be solved. 2. You made a typo in this line: $filename="count.txt"; $handle = fopen($filname,"r"); Change to: $handle = fopen($filname,"r"); You wrote "$filname". It should be "$filename" Do these, and it should work. Enjoy! When coding on wamp, keep your error reporting ON, It will catch a lot of errors like that $filename typo etc.