Step 1 – 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:
$ip = $_SERVER['REMOTE_ADDR'];
PHP F1
echo "Remote addr: " . $_SERVER['REMOTE_ADDR']."
";
echo "X Forward: " . $_SERVER['HTTP_X_FORWARDED_FOR']."
";
echo "Clien IP: " . $_SERVER['HTTP_CLIENT_IP']."
";
Using this information is quite easy now to create a simple function which returns the probably real ip of the site visitor:
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;
}
ConversionConversion EmoticonEmoticon