simon66 Posted May 11, 2010 Share Posted May 11, 2010 Hi all, This is my first php file im working on so bear with me. What Im doing here is that I want my php script to add the users IP to a file without deleting the files inside of it and if the IP is already in the file then it wont add it This is what I have to far <?php $ip = getenv("REMOTE_ADDR") ; $file = fopen("ip.txt", "w+"); fwrite($file, $ip); fclose($file); ?> the problem there is it will not add to the file, it will delete what ever is inside of the it and just add the new IP. Plus I still to add the search. If some one could help It would be amazing Ill be looking around to see if I can do a search Link to comment https://forums.phpfreaks.com/topic/201329-writing-file-help/ Share on other sites More sharing options...
kenrbnsn Posted May 11, 2010 Share Posted May 11, 2010 Use the "a" mode to open the file for append. <?php $file = fopen("ip.txt", "a"); ?> Ken Link to comment https://forums.phpfreaks.com/topic/201329-writing-file-help/#findComment-1056224 Share on other sites More sharing options...
sharp.mac Posted May 11, 2010 Share Posted May 11, 2010 It way also help to take the current contents of the file, store it as a temp variable, then rewrite the file, though this will get rather large if your going to be logging everyone who goes to your site. Link to comment https://forums.phpfreaks.com/topic/201329-writing-file-help/#findComment-1056349 Share on other sites More sharing options...
simon66 Posted May 12, 2010 Author Share Posted May 12, 2010 It way also help to take the current contents of the file, store it as a temp variable, then rewrite the file, though this will get rather large if your going to be logging everyone who goes to your site. How can I do that? Is it better if I store his IP in a Database? Link to comment https://forums.phpfreaks.com/topic/201329-writing-file-help/#findComment-1056917 Share on other sites More sharing options...
teamatomic Posted May 12, 2010 Share Posted May 12, 2010 To use a flat file you could something like this: $ip = getenv("REMOTE_ADDR"); $lines=file("./ip.txt"); while (list($key,$value) = each($lines)) { $value=trim($value); if($value==$ip){$found=1; break;} } if(!$found) { $write="$ip\n"; file_put_contents("./ip.txt",$write,FILE_APPEND); } What we are doing is putting the ip.txt file into an array then looping with a while/list/each. We use that cause as the file gets larger its a hit to use a foreach loop as a copy is used but a while/list/each goes line by line through the original array. We break if we find a match otherwise if we didnt find a match we append the IP to the file. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/201329-writing-file-help/#findComment-1056945 Share on other sites More sharing options...
litebearer Posted May 12, 2010 Share Posted May 12, 2010 Perhaps... ####################################### # file_put_contents is PHP 5> # # this piece checks to see if that function exists # if NOT creates the function ####################################### if (!function_exists('file_put_contents')) { function file_put_contents($filename, $data) { $f = @fopen($filename, 'w'); if (!$f) { return false; } else { $bytes = fwrite($f, $data); fclose($f); return $bytes; } } } $file_name = "ip.txt"; $needle = getenv("REMOTE_ADDR"); if(($pos = strpos(file_get_contents($file_name), $needle))===false) { file_put_contents($file_name, file_get_contents($file_name) . $needle . "\n"); } N... Link to comment https://forums.phpfreaks.com/topic/201329-writing-file-help/#findComment-1056956 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.