Created
September 28, 2014 11:23
-
-
Save mrkpatchaa/d62558a1ae0e1f1f7d04 to your computer and use it in GitHub Desktop.
Get user ip address in php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?PHP | |
function getUserIP() | |
{ | |
$client = @$_SERVER['HTTP_CLIENT_IP']; | |
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR']; | |
$remote = $_SERVER['REMOTE_ADDR']; | |
if(filter_var($client, FILTER_VALIDATE_IP)) | |
{ | |
$ip = $client; | |
} | |
elseif(filter_var($forward, FILTER_VALIDATE_IP)) | |
{ | |
$ip = $forward; | |
} | |
else | |
{ | |
$ip = $remote; | |
} | |
return $ip; | |
} | |
$user_ip = getUserIP(); | |
echo $user_ip; // Output IP address [Ex: 177.87.193.134] | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment