countrydj Posted July 14, 2016 Share Posted July 14, 2016 Hi Guys...I want to collect ip numbers from submitted forms and store them into a text file.this is what I've got up to now: # get the submitters IP address $remote_ip = getenv(REMOTE_ADDR); $ip_file = fopen("inbritain-ip-file.txt",'a') or die("Unable to open file!"); fwrite($ip_file, $remote_ip); fclose($ip_file); This works fine and collects the ip numbers, BUT it adds then on to the end of the file.e.g. 88.97.199.15088.97.199.15088.97.199.15088.97.199.150I need them in a list.e.g.88.97.199.15088.97.199.15088.97.199.15088.97.199.150etc.I can't figure out how to get the code to do that.Has anybody got any ideas?Thanks, John C Quote Link to comment https://forums.phpfreaks.com/topic/301482-how-to-collect-ip-numbers-into-a-list/ Share on other sites More sharing options...
ginerjm Posted July 14, 2016 Share Posted July 14, 2016 1. - Why a text file? Why not a db table? Do you paln on doing anything with this data? A table will be much easier to search thru. 2 - if you really want them on new lines, add a new line char. $crlf = "\n\r"; Add this var to your output when you do the write. Quote Link to comment https://forums.phpfreaks.com/topic/301482-how-to-collect-ip-numbers-into-a-list/#findComment-1534517 Share on other sites More sharing options...
Zane Posted July 15, 2016 Share Posted July 15, 2016 Change this line fwrite($ip_file, $remote_ip . "\r\n"); Or change this line $remote_ip = getenv(REMOTE_ADDR). "\r\n"; The \r represents a carriage return (CR), while the \n represents an actual new line, or line feed (LF). This also means, though, that you will have a hanging/straggling newline at the bottom of your file. Quote Link to comment https://forums.phpfreaks.com/topic/301482-how-to-collect-ip-numbers-into-a-list/#findComment-1534518 Share on other sites More sharing options...
ginerjm Posted July 15, 2016 Share Posted July 15, 2016 What will you do with duplicate ip numbers? Kind of hard to prevent that with a text file as a database. Quote Link to comment https://forums.phpfreaks.com/topic/301482-how-to-collect-ip-numbers-into-a-list/#findComment-1534533 Share on other sites More sharing options...
Zane Posted July 15, 2016 Share Posted July 15, 2016 What will you do with duplicate ip numbers? Kind of hard to prevent that with a text file as a database. Not necessarily. One could use a combination of the file() function and in_array() function to prevent duplicates. // Completely untested!! # get the submitters IP address $remote_ip = getenv(REMOTE_ADDR). "\r\n"; $filename = "inbritain-ip-file.txt"; $theFile = file($filename); $fileRef = fopen($filename) or die("Unable to open file!"); foreach($theFile as $ip) { if( !in_array($remote_ip, $theFile) ) fwrite($fileRef, $remote_ip); } fclose($fileRef); Quote Link to comment https://forums.phpfreaks.com/topic/301482-how-to-collect-ip-numbers-into-a-list/#findComment-1534542 Share on other sites More sharing options...
Jacques1 Posted July 15, 2016 Share Posted July 15, 2016 The file must be locked, otherwise concurrent processes are free to trample over each other, and the file content is nondeterministic. So a proper flat-file database actually requires a lot of code, and then it's still just a lame hack compared to an actual database. Why not use the SQL database on your server as suggested by ginerjm? PostgreSQL even has data types specifically for IP addresses. And if you don't have any SQL database at hand, there's still SQLite. Another problem is the way you fetch the client's IP address: getenv(REMOTE_ADDR) There's no such thing as REMOTE_ADDR. If you fix your error reporting, you'll get a notice about an undefined constant. The only reason why this works at all is because PHP assumes you meant the string "REMOTE_ADDR". Don't rely on that error correction; if you want a string, use a string. Using this environment variable is an odd approach and appearently only works in specific environments. You should use the $_SERVER superglobal. The REMOTE_ADDR will be “wrong” if the client is behind a proxy server. If you want a more reliable result, you need to check other fields as well like $_SERVER['HTTP_X_FORWARDED_FOR'] Quote Link to comment https://forums.phpfreaks.com/topic/301482-how-to-collect-ip-numbers-into-a-list/#findComment-1534544 Share on other sites More sharing options...
ginerjm Posted July 15, 2016 Share Posted July 15, 2016 All very well and good Zane, but the point was to steer the OP towards a more proper "database" approach. Quote Link to comment https://forums.phpfreaks.com/topic/301482-how-to-collect-ip-numbers-into-a-list/#findComment-1534545 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.