Created
November 16, 2018 02:31
-
-
Save komputronika/58970d169200096e2fa72d4369ca0e3a to your computer and use it in GitHub Desktop.
Get user's IP address with PHP
This file contains hidden or 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 user_ip_address() { | |
if( array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ) { | |
if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',')>0) { | |
$addr = explode(",",$_SERVER['HTTP_X_FORWARDED_FOR']); | |
$ip = filter_var(trim($addr[0]), FILTER_VALIDATE_IP) | |
return $ip; | |
} else { | |
return $_SERVER['HTTP_X_FORWARDED_FOR']; | |
} | |
} else { | |
return $_SERVER['REMOTE_ADDR']; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reference: https://stackoverflow.com/questions/13646690/how-to-get-real-ip-from-visitor