Papa-Beans Posted October 30, 2013 Share Posted October 30, 2013 Hello! l was wondering if you might be able to help me. l am trying to find out how to make PHP read a file, and then check if that text is already there, and if so don't re-write it. l'll give out my code for an example. <?php //text file that counts the peeps that visit the site $lne = "line.txt"; $File = "counter.txt"; $handle = fopen($File, 'r+') ; //file open $data = fread($handle, 512) ; //file read $count = $data + 1; //Adds one person :DDDDD fseek($handle, 0) ; fwrite($handle, $count) ; //saves the new person echo 'Count #'.$count; // // // // // // $File2 = "ips.txt"; fclose($handle) ; if (isset($_POST['ip'])) { $ip = $_POST['ip']; $port = $_POST['port']; echo 'Connecting to ' . $ip . ':' . $port; exit(); } $ip = getenv("REMOTE_ADDR"); echo '<br>'.$ip; $handle2 = fopen($File2, 'r+') ; //file open $data2 = fread($handle2, 512) ; //file read fwrite($handle2,"\n" . $ip .':' . $count); //saves the new ip ?> <html> <form method="POST"> <input type="hidden" name="ip"> <input type="hidden" name="port"> </html> lt just re-writes the lP with the new counter, very annoying. l can filter it out with VB but l want it all to be server sided. Thank you for your time ~Papa Beans Quote Link to comment https://forums.phpfreaks.com/topic/283440-php-file-writing-help/ Share on other sites More sharing options...
Rifts Posted October 30, 2013 Share Posted October 30, 2013 whats not working? any errors? Quote Link to comment https://forums.phpfreaks.com/topic/283440-php-file-writing-help/#findComment-1456216 Share on other sites More sharing options...
Barand Posted October 30, 2013 Share Posted October 30, 2013 try $file = "counter.txt"; $count = (file_exists($file)) ? file_get_contents($file)+1 : 1; file_put_contents($file, $count); Quote Link to comment https://forums.phpfreaks.com/topic/283440-php-file-writing-help/#findComment-1456224 Share on other sites More sharing options...
DavidAM Posted October 30, 2013 Share Posted October 30, 2013 It sounds like you are asking how to not write the visitor's IP Address to the second file if it is already there. This would be a simple matter except for the fact that you are appending the counter to the IP address, why is that? Anyway, without the counter added, it would go something like this: $ip = $_SERVER["REMOTE_ADDR"]; if (file_exists($File2)) $ipList = file($File2, FILE_IGNORE_NEW_LINES); else $ipList = array(); if (!in_array($ip, $ipList)) { file_put_contents($File2, $ip . "\n", FILE_APPEND | LOCK_EX); } If you really need/want the :$counter appended to the IP Address, you will have to walk the array ($ipList) and compare the IP address part with the user's IP address. If you get through the entire array and don't find the user's IP, then you would add it. Note: the code above requires PHP 5.0 (for some of the flags). Note: Using file storage like this is not best practice. You are single-threading through the file, which means if multiple users hit the code at the same time, they will have to wait for the others to complete. Also, if two users from the same IP address read the file at the same time, and neither finds their IP in the file they will BOTH add it, causing duplication. You can attempt to use fopen, fread, fwrite, and fclose, to keep the file locked, but again that leads to delays for the user. This type of thing is best done with a database. Quote Link to comment https://forums.phpfreaks.com/topic/283440-php-file-writing-help/#findComment-1456234 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.