小小酥 2017-11-21 2388
可以避免各种CDN对获取真实用户IP的影响
function get_ip() { $iplib = ["HTTP_ALI_CDN_REAL_IP", "HTTP_TRUE_CLIENT_IP", "HTTP_X_REAL_FORWARDED_FOR", "HTTP_X_CONNECTING_IP", "HTTP_CF_CONNECTING_IP", "HTTP_X_FORWARD_FOR", "HTTP_X_REAL_IP", "HTTP_X_FORWARDED_FOR", "REMOTE_ADDR"]; foreach ($iplib as $val) { if (isset($_SERVER[$val]) && $_SERVER[$val] && strcasecmp($_SERVER[$val], "unknown")) { $ips = explode(',', $_SERVER[$val]); $ip = $ips[0]; break; } } if ($ip && filter_var($ip, FILTER_VALIDATE_IP)) { return $ip; } return ""; }