function fn_get_ip($return_int = false) { $forwarded_ip = ''; $fields = array('HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'HTTP_forwarded_ip', 'HTTP_X_COMING_FROM', 'HTTP_COMING_FROM', 'HTTP_CLIENT_IP', 'HTTP_VIA', 'HTTP_XROXY_CONNECTION', 'HTTP_PROXY_CONNECTION'); $matches = array(); foreach ($fields as $f) { if (!empty($_SERVER[$f])) { preg_match("/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $_SERVER[$f], $matches); if (!empty($matches) && !empty($matches[0]) && $matches[0] != $_SERVER['REMOTE_ADDR']) { $forwarded_ip = $matches[0]; break; } } } $ip = array('host' => $forwarded_ip, 'proxy' => $_SERVER['REMOTE_ADDR']); if ($return_int) { foreach ($ip as $k => $_ip) { $ip[$k] = empty($_ip) ? 0 : sprintf("%u", ip2long($_ip)); } } if (empty($ip['host']) || !fn_is_inet_ip($ip['host'], $return_int)) { $ip['host'] = $ip['proxy']; $ip['proxy'] = $return_int ? 0 : ''; } return $ip; }
function fn_stat_ip_exist($ip) { if (!empty($ip['host']) && fn_is_inet_ip($ip['host'], true)) { $ip_num = $ip['host']; } elseif (!empty($ip['proxy']) && fn_is_inet_ip($ip['proxy'], true)) { $ip_num = $ip['proxy']; } $ip_id = isset($ip_num) ? db_get_field("SELECT ip_id FROM ?:stat_ips WHERE ip = ?i", $ip_num) : false; if (empty($ip_id) && !empty($ip_num)) { $ip_id = fn_stat_save_ip(array('ip' => $ip_num)); } return empty($ip_id) ? false : $ip_id; }