How to Get visitors IP address with PHP

by khumaar on March 18, 2011, 08:40:05 AM
Pages: [1]
Print
Author  (Read 4267 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?
December 25, 2024, 08:22:58 AM

Login with username, password and session length
Recent Replies
by mkv
[December 22, 2024, 05:36:15 PM]

[December 19, 2024, 08:27:42 AM]

[December 17, 2024, 08:39:55 AM]

[December 15, 2024, 06:04:49 AM]

[December 13, 2024, 06:54:09 AM]

[December 10, 2024, 08:23:12 AM]

[December 10, 2024, 08:22:15 AM]

by Arif Uddin
[December 03, 2024, 07:06:48 PM]

[November 26, 2024, 08:47:05 AM]

[November 21, 2024, 09:01:29 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.112 seconds with 23 queries.
[x] Join now community of 8509 Real Poets and poetry admirer