Getting user's IP
Moderator: General Moderators
Getting user's IP
I want my scripts to be able to see the user's IP address (security), but I can't find out how. I think it can be done, so could someone please tell me if it can and how, or if it can't?
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Try:
Mac
Code: Select all
$_SERVERї'REMOTE_ADDR'];Another way
Another way is to use just:-
Code: Select all
$REMOTE_ADDR- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
$_SERVER['REMOTE_ADDR'] will work with PHP 4.1 and above and register globals on or off. $HTTP_SERVER_VARS['REMOTE_ADDR'] will work with pretty much any version of PHP and register globals on or off. $REMOTE_ADDR will work with pretty much any version of PHP but only when register globals is on. You need to choose the one which makes the most sense, in that do you want it to be future proof and should it be backwards compatible.
Mac
Mac
Depending on your needs, you can check $_SERVER["HTTP_X_FORWARDED_FOR"]. If its set, then it normally contains the users real IP, while $_SERVER["REMOTE_ADDR"] would hold the proxy's IP.
If you're worried about spoofing, doing a double name lookup can make it harder for the spoofer, but it will also slow your script down.
If you're worried about spoofing, doing a double name lookup can make it harder for the spoofer, but it will also slow your script down.
Code: Select all
function testIP($ip)
{
$host = gethostbyaddr($ip);
if ($host != $ip) // make sure we got a name
{
$ip2 = gethostbyname($host);
return $ip2 == $ip;
}
return FALSE;
}