How to Get visitors IP address with PHP

by khumaar on March 18, 2011, 08:40:05 AM
Pages: [1]
Print
Author  (Read 4190 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?
April 26, 2024, 06:18:33 AM

Login with username, password and session length
Recent Replies
[April 23, 2024, 09:54:09 AM]

by ASIF
[April 22, 2024, 01:50:33 PM]

[April 04, 2024, 04:49:28 PM]

[April 02, 2024, 12:27:12 PM]

by ASIF
[March 24, 2024, 04:34:54 AM]

by ASIF
[March 24, 2024, 04:30:44 AM]

by ASIF
[March 24, 2024, 04:26:39 AM]

by ASIF
[March 23, 2024, 08:50:46 AM]

[March 21, 2024, 07:59:38 PM]

[March 17, 2024, 02:01:29 PM]
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.154 seconds with 23 queries.
[x] Join now community of 8498 Real Poets and poetry admirer