Sinisterly
How To Grab IP Address With PHP - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: PHP (https://sinister.ly/Forum-PHP)
+--- Thread: How To Grab IP Address With PHP (/Thread-How-To-Grab-IP-Address-With-PHP)

Pages: 1 2


How To Grab IP Address With PHP - Woody - 06-16-2013

Introduction

In this tutorial, I'll be showing you how to grab someone's IP address using PHP. Unlike most tutorials, I'll be showing you what each part of the code means! Biggrin
The Simple Way

This PHP code will simply print the visitor's IP address onto the page.
PHP Code:
<?php print ($_SERVER['REMOTE_ADDR']; //prints the IP on the page (Obviously) ?>

Printing The Visitor's IP Address To A File

PHP Code:
<?php $ip = $_SERVER['REMOTE_ADDR']; //sets the visitors IP ($_SERVER['REMOTE_ADDR']) to the variable "$ip" $handle = fopen('ipaddresses.txt', 'a+'); //opens ipaddresses.txt for reading and writing (MUST HAVE FILE NAMED ipaddresses.txt) fwrite($handle, "{$ip}\n"); //writes the IP address to ipaddresses.txt fclose($handle); //closes ipaddresses.txt for reading and writing ?>

Putting Your Code Into A Index.php File

PHP Code:
<html> <?php $file = "ipaddresses.txt"; //turns the text file into a variable $f=fopen($file, 'a+'); //opens up the text file for reading and writing fwrite($f,$_SERVER['REMOTE_ADDR']."\n"); //writes the IP address to ipaddresses.txt fclose($f); //closes ipaddresses.txt for reading and writing ?> <h1>File Not Found</h1> </body> </html>



RE: How To Grab IP Address With PHP - RogueCoder - 06-16-2013

Nice little guide there Smile Thanks for sharing. One little thing though, you have one extra single-quote in the second code block Wink


RE: How To Grab IP Address With PHP - Woody - 06-16-2013

(06-16-2013, 10:32 AM)shp0ngl3 Wrote: Nice little guide there Smile Thanks for sharing. One little thing though, you have one extra single-quote in the second code block Wink

Thanks, and oh thanks for that too! I didn't even notice it! Preshate it bro!
Fixed


RE: How To Grab IP Address With PHP - Anima Templi - 06-16-2013

One little thing I want to add, please edit the formatation of the headers. They are too big and there's no need to make it in red.


RE: How To Grab IP Address With PHP - Woody - 06-16-2013

Fixed.


RE: How To Grab IP Address With PHP - zomgwtfbbq - 06-16-2013

Yeah nice, this tutorial has only been posted 3 times on the first page of this subforum..


RE: How To Grab IP Address With PHP - noize - 06-16-2013

Have you never been to some "what's-your-IP" website which told you a wrong IP instead of yours? That's because of some IP grabbing code not working for you. Your code might work wrong for some users, this should work everytime:

Code:
if (getenv(HTTP_X_FORWARDED_FOR)) { $ip = getenv(HTTP_X_FORWARDED_FOR); } else { $ip = getenv(REMOTE_ADDR); }



RE: How To Grab IP Address With PHP - Woody - 06-16-2013

Oh yeah it's not like I didn't see it or anything.

Watch your language, next time I won't let you slip with a warning. - Anima Templi


RE: How To Grab IP Address With PHP - pratham - 06-16-2013

Good sharing brother


RE: How To Grab IP Address With PHP - Woody - 06-16-2013

Thank you.