Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Obtaining a visitors IP adress


Obtaining a visitors IP adress

By ghostghost | 5333 Reads |
0     0

ok i had some problems the other day with someone using a email spoofer i created in php against me. My page originally wasnt able to get the ip of visitors to the page so i had no way of tracking them down.

Recently though one of my friends created this bit of code and now the ip adresses of all people who visit the page will be stored in a text file in the directory.

Here is the code: <? $ipFile = "ip.txt"; // text file that stores IP addresses $fp_ips = fopen($ipFile, "r"); // Open the file $ip = fgets($fp_ips, 20); // Get the IPs stored already fclose($fp_ips); // close the file $fp_ips = fopen($ipFile, "a"); // open it again so we start at line 0 (first one) fputs($fp_ips, $REMOTE_ADDR . "\r\n"); // add the visiting IP address + carriage return + new line fclose($fp_ips); // close the file ?>

Its pretty self explanatory, with the //comments there.

So what you need to do to get this to work though, is create a text document ip.txt and set both this page and the ip.txt page to chmod 777 so it can be written to.

Thats pretty much it and i hope you find this useful. Im just awaiting the moment the person uses my page again so i can nail them.

Please vote and leave comments. Thanks -Frozen Flame

Comments
ghost's avatar
ghost 18 years ago

great code! hope you catch them =D

ghost's avatar
ghost 18 years ago

See, I've done code like that then but i also made it log their visit like how long they stay but great code btw the commens make it look messy i was like oO whats wrong with this guy

ghost's avatar
ghost 18 years ago

lol sorry about that. just trying to make it easily understandable what everything does. thanks for the comments btw

ghost's avatar
ghost 18 years ago

Umm what do you mean by "set both this page and the ip.txt page to chmod 777 so it can be written to" Does ip.txt have to be empty, and what is chmod777. New to this.

ghost's avatar
ghost 18 years ago

I hope you can find them and this is a good example of something simple. =)

SySTeM's avatar
SySTeM 18 years ago

Nice article

ghost's avatar
ghost 18 years ago

Faucet, when you chmod to 777, you give all permissions on a folder (read/write/execute). This can be done by right clicking the file and clicking properties. However, if you are using windows, don't worry about it since it is insecure and will let you write to the file even if you shouldn't have permission to.

Frozen Flame: The code is pretty good for beginners, but to optimize it so it runs faster and only has to open the file once, I would simply do -

&lt;?php
$fp = fopen(&#39;ip.txt&#39;, &#39;a+&#39;);
  fputs($fp, $_SERVER[&#39;REMOTE_ADDR&#39;].&quot;&#92;r&#92;n&quot;);
  fclose($fp);
?&gt;

a+ means it will open the file for reading and writing, the a stands for 'append' and it is better to use the longer version for IP because not all servers have the shorthand enabled.

ghost's avatar
ghost 18 years ago

Stupid UBB screwed up the code… should still work though, just insert line breaks after the ';' to make it look nice.

ghost's avatar
ghost 17 years ago

erm… this might be a noobish comment, but all I get in ip.txt is 'rn' - why? where is the IP?

ghost's avatar
ghost 16 years ago

pretty nice code ;)