Created
April 29, 2019 14:41
-
-
Save kreativan/93ec2af2da7c8b0130da7a92397b2dc1 to your computer and use it in GitHub Desktop.
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() { | |
// Get real visitor IP behind CloudFlare network | |
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) { | |
$_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"]; | |
$_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"]; | |
} | |
$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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment