Jump to content

How to save somebody's IP in text file


canadezo

Recommended Posts

What can I do to make it so only the first person that viewed the page gets their IP saved in a single text file, the purpose is to have IP validation instead of password... (I understand the consequences, no need in explaining that)

So; when they click to the page they're directed to, it saves their IP in a text file (or that same file if possible)... Any idea's?

Link to comment
Share on other sites

Get their IP address, put it into an array, count the array, if less than one, write IP.

 

 

$a=array(IP DETAILS)
if(count($a) < 1){
Do write IP.
}
else{
//Leave Blank.
}

 

Use fwrite to write the IP address. Don't know about putting into the same file. I don't know exactly how to process the IP stuff, and how store it in the array. But might get you started.

Link to comment
Share on other sites

Get their IP address, put it into an array, count the array, if less than one, write IP.

 

 

$a=array(IP DETAILS)
if(count($a) < 1){
Do write IP.
}
else{
//Leave Blank.
}

 

Use fwrite to write the IP address. Don't know about putting into the same file. I don't know exactly how to process the IP stuff, and how store it in the array. But might get you started.

 

That's excellent, straight forward, I love it. I'm going to use that; and save myself a lot of time. =)

Link to comment
Share on other sites

that code will never write the IP since all you do is set $a to an array containing the IP details on one person. The count for this array will always be 1, and thus the condition will always be false.

 

That being said, I'm not sure what exactly you are trying to accomplish. Do you want to basically store someone's IP when they get to a "login" page once, but not store it again if their IP is already stored? If so you will need to first get the contents of the text file, make sure that the IP is not in there, and then write to it if not. However, i'm not entirely sure why you aren't using a database. Besides the fact that this is a terrible way to log someone in, it would be alot simpler with a database. but something along the lines of

$ip = ...;//however you get the IP. probably from the $_SERVER super global
$lines = file("path/to/file.txt");
$found = false;//flag to decide if we have found the IP or not
foreach($lines as $line){
if ($line == $ip){
$found = true;
break;
}
}

if (!$found){//if ip isnt in file
$handle = fopen($filename, 'a');
fwrite($handle, $ip."\n");
}

 

But remember, as I said, I strongly recommend using databases as its much easier and will cause less strain on your system.

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.