Getting IP Address In PHP

by Hiroshi on June 13, 2008

in IP Address

How do I get a user’s (Visitor’s) IP address in PHP?

The users ip address is stored in the environment variable $REMOTE_ADDR, if you want to resolve the domain name for that ip (if any) use:

<?php
$domain = GetHostByName($REMOTE_ADDR);
?>

Usage (Code)

My IP address is:

<?php
$domain = GetHostByName($REMOTE_ADDR);
echo($domain);
?>

OR u can use following:

<?php
if (getenv('HTTP_X_FORWARDED_FOR')) {
$ip=getenv('HTTP_X_FORWARDED_FOR');
}
else {
$ip=getenv('REMOTE_ADDR');
}
echo $ip;
?>

Simply put this code into your web page and save webpage in php extension and you will get ip address of user.

Download IP Address Example

Related Posts

Previous post:

Next post: