Jump to content

How to collect ip numbers into a list


countrydj

Recommended Posts

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.150
I need them in a list.
e.g.
88.97.199.150
88.97.199.150
88.97.199.150
88.97.199.150
etc.

I can't figure out how to get the code to do that.
Has anybody got any ideas?

Thanks,

 

John C

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
Link to comment
Share on other sites

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']
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.