This is a discussion on visitor's IP Address within the PHP Programming forums, part of the Web Development category; Hi, How to Find a visitor's IP Address with PHP Regards, sivaraman...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi, In a PHP page, within the PHP tags, <?php ... ?>, you can retrieve the IP address of a user through the server variables array, which is contains information about the user and the server environment and is accessible from anywhere in your PHP script. To do this, you use the code $_SERVER['REMOTE_ADDR']. It works like a normal array, where REMOTE_ADDR is the key, and the IP address of the visitor is the value associated with that key. Display The IP Address So, if you want to display the IP Address to the user then the following page will suffice: <?php echo "Hello! Your IP Address is: " . $_SERVER['REMOTE_ADDR']; ?> Exploring the $_SERVER array You might be wondering what other information you can get from $_SERVER, well you can find out with the following script which displays all the variables in it along with their values if set, in a HTML table: <table border="1"> <tr><th>Variable</th><th>Value</th></tr> <? foreach( $_SERVER as $key => $value ) { echo "<tr><td>" . $key . "</td>"; echo "<td>" . $value . "</td></tr>"; } ?> </table> Finally, the following code works harder to find the true IP of the user by checking for proxies: function userIP() { //This returns the True IP of the client calling the requested page // Checks to see if HTTP_X_FORWARDED_FOR // has a value then the client is operating via a proxy $userIP = $_SERVER['HTTP_X_FORWARDED_FOR']; if($userIP == "") { $userIP = $_SERVER['REMOTE_ADDR']; } // return the IP we've figured out: return $userIP; }
__________________ With, J. Jeyaseelan Everything Possible |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Same IP Address | vadivelanvaidyanathan | Search Engine Optimization | 0 | 01-23-2008 06:28 AM |
| Getting IP address with PHP | sureshbabu | PHP Programming | 0 | 12-17-2007 07:01 AM |
| How to resize a visitor's browser? | jeyaprakash.c | HTML, CSS and Javascript Coding Techniques | 4 | 10-14-2007 10:20 PM |
| How to get the MAC Address By IP address in .net? | oxygen | ASP and ASP.NET Programming | 1 | 07-27-2007 05:13 AM |
| Visitor's browser information | venkat_charya | PHP Programming | 2 | 07-19-2007 03:50 AM |