How to Get visitors IP address with PHP

by khumaar on March 18, 2011, 08:40:05 AM
Pages: [1]
Print
Author  (Read 4255 times)
khumaar
Guest
Get visitors IP address with PHP


Get visitors IP address with PHP


Getting a visitors IP address can be important for a lot of reasons, for example, logging, geo targeting, redirecting the user and so on. All of the IP relevant informations can be found in the $_SERVER array. The simplest way to get the visitors IP address is as simple as the following code, by reading the REMOTE_ADDR field:

Code:
$ip = $_SERVER['REMOTE_ADDR'];PHP F1

However this solution is not completely accurate, as if the user sits behind a proxy, then you will get the IP of the proxy server and not the real user address. Fortunately we can make some additional refinement to get more accurate results. Proxy servers extend the HTTP header with new property which stores the original IP. The name of this filed is X-Forwarded-For or Client-Ip. If one of these fields are present in the HTTP header then you can read their values from the $_SERVER array as in the first example. So you need to check all the 3 possibilities:

Code:
echo "Remote addr: " . $_SERVER['REMOTE_ADDR']."<br/>";echo "X Forward: " . $_SERVER['HTTP_X_FORWARDED_FOR']."<br/>";echo "Clien IP: " . $_SERVER['HTTP_CLIENT_IP']."<br/>"; PHP F1
Using this information is quite easy now to create a simple function which returns the probably real ip of the site visitor:

Code:
function getIp() {    $ip = $_SERVER['REMOTE_ADDR'];     if (!empty($_SERVER['HTTP_CLIENT_IP'])) {        $ip = $_SERVER['HTTP_CLIENT_IP'];    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];    }     return $ip;}
Logged
Similar Poetry and Posts (Note: Find replies to above post after the related posts and poetry)
:: asking a girl's address and phone no :: by myheart_ursonly in Chit - Chat & General Discussion « 1 2 ... 7 8 »
@ in email address by qayda in Technology & Computers
Write your hotmail or yahoo address by Azedmeer in Chit - Chat & General Discussion
How to Change the IP address by @kaash in Technology & Computers
How to make anonymous IP address by khumaar in Technology & Computers
AbhiTamrakar
Guest
«Reply #1 on: March 18, 2011, 09:54:38 AM »
nice info ...

only the function is useful...the code is just a basic utility which provide three different address...where Xforward and client ip will produce same values at an instant..




Code:
echo "Remote addr: " . $_SERVER['REMOTE_ADDR']."<br/>";echo "X Forward: " . $_SERVER['HTTP_X_FORWARDED_FOR']."<br/>";echo "Clien IP: " . $_SERVER['HTTP_CLIENT_IP']."<br/>"; PHP F1
Using this information is quite easy now to create a simple function which returns the probably real ip of the site visitor:

Code:
function getIp() {    $ip = $_SERVER['REMOTE_ADDR'];     if (!empty($_SERVER['HTTP_CLIENT_IP'])) {        $ip = $_SERVER['HTTP_CLIENT_IP'];    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];    }     return $ip;}
 
Logged
Pages: [1]
Print
Jump to:  


Get Yoindia Updates in Email.

Enter your email address:

Ask any question to expert on eTI community..
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 21, 2024, 04:28:02 PM

Login with username, password and session length
Recent Replies
[November 21, 2024, 09:01:29 AM]

[November 16, 2024, 11:44:41 AM]

by Michaelraw
[November 13, 2024, 12:59:11 PM]

[November 08, 2024, 09:59:54 AM]

[November 07, 2024, 01:56:50 PM]

[November 07, 2024, 01:55:03 PM]

[November 07, 2024, 01:52:40 PM]

[November 07, 2024, 01:51:59 PM]

[October 30, 2024, 05:13:27 AM]

by ASIF
[October 29, 2024, 07:57:46 AM]
Yoindia Shayariadab Copyright © MGCyber Group All Rights Reserved
Terms of Use| Privacy Policy Powered by PHP MySQL SMF© Simple Machines LLC
Page created in 0.096 seconds with 22 queries.
[x] Join now community of 8506 Real Poets and poetry admirer