Find a visitor’s IP Address with PHP
Published 2 years, 11 months ago in Basics and PHPDo you want to know the IP address of a visitor? This can be useful for many reasons, such as tracking site usage or blocking access to specific people. Here's how you can find it using PHP.
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;
-
}
If you have a low-traffic website, consider joining the Money4Banners advertising network. They will pay you £10 + £5 each month for displaying a small advert on three of your pages, regardless of traffic. American webmasters are welcome, and since £10 == $20, you make more!
6 Responses to “Find a visitor’s IP Address with PHP”
Leave a Reply
You must log in to post a comment.
Search
Categories
- ASP (3)
- ASP Basics (3)
- Blogs (2)
- C# (1)
- deals (1)
- Firefox (1)
- Flash (1)
- Google (6)
- Hacks (4)
- Java (1)
- PHP (12)
- Basics (8)
- Files (1)
- Image Handling (3)
- Puzzles (1)
- Ruby (1)
- Tips (10)
- Web Hosting (3)
- Web Hosting Articles (1)
- Web Hosting News (2)
- Windows (6)
- Wordpress (2)
Related Entries
- Find a visitor's IP Address in ASP
- PHP
- How to Find the Current URL with PHP
- Finding Items in an Array with PHP
- How to use PHP in pages with a .html extension (or any other)
- Generating Random Numbers in PHP
- How to Access a Page in Google's Cache
- Hide a hard drive from My Computer
- How to use the query string in PHP
- The Difference Between require() and include()
I need to save the ip address in some
table for my own use
not just to show the visitor
I need to save the ip adress to…
Just use the $userIP and save the value in the table.
It works only on local network :o( Whene You are behind proxy or firewall You will get IP of them :o(
Client IP address =$REMOTE_ADDR
Client Browser = $HTTP_USER_AGENT
Referral Site = $HTTP_REFERER \n\n\r
What versions of PHP will this work for…? PHP 4.x or only PHP 5