Is there a way to record the IP address of users? I would like to be able to verify the geographic location of a users.
| SitePoint Sponsor |



Is there a way to record the IP address of users? I would like to be able to verify the geographic location of a users.
it depends what you have got on your server
if you are using PHP the array element $_SERVER['REMOTE_ADDR'] contains The IP address from which the user is viewing the current page.



I am using html and cgi mail for form processing.



Here's a complete PHP function to return the correct user IP address:
To use it:PHP Code:function return_user_ip()
{
$ip = FALSE;
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ips = explode (', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip)
{
array_unshift($ips, $ip);
$ip = FALSE;
}
for ($i = 0; $i < count($ips); $i++)
{
$long = ip2long($ips[$i]);
// RFC1918 (10.0.0.0/8, 192.168.0.0/16 and 172.16.0.0/12)
if (!preg_match('^(10|192\.168)\.', $ips[$i]) AND
!($long >= -1408237568 AND $long <= -1407188993))
{
if (version_compare(phpversion(), '5.0.0', '>='))
{
if ($long != false)
{
$ip = $ips[$i];
break;
}
}
else
{
if ($long != -1)
{
$ip = $ips[$i];
break;
}
}
}
}
}
return ($ip) ? $ip : $_SERVER['REMOTE_ADDR'];
}
After you have the IP address, check it against a geo-location database like ip-to-country.com and you're all setPHP Code:$user_ip = return_user_ip();
![]()
~ Daniel Macedo



You can have an .htaccess to allow you to execute PHP inside the .cgi or even .htmlOriginally Posted by Manpasand
Just place:
in your .htaccess and in the .cgi you can now use the code aboveCode:AddType application/x-httpd-php .cgi
This assumes you're using Apache and your host has PHP installed![]()
~ Daniel Macedo



Thanks for your kind help.
Bookmarks