Created
February 13, 2013 09:46
-
-
Save artdevue/4943417 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 | |
/** | |
* User IP | |
* | |
* @category validate | |
* @version 0.1 | |
* @license GNU General Public License (GPL), http://www.gnu.org/copyleft/gpl.html | |
* @param string $out IP адрес который будет отдан функцией, если больше ничего не обнаружено | |
* @return string IP пользователя | |
* @author Agel_Nash <Agel_Nash@xaker.ru> | |
* | |
* @see http://stackoverflow.com/questions/5036443/php-how-to-block-proxies-from-my-site | |
*/ | |
function getUserIP($out='127.0.0.1'){ | |
//see: http://www.php.net/manual/ru/functions.anonymous.php | |
$getEnv = function($data){ | |
$out=false; | |
switch(true){ | |
case (isset($_SERVER[$data])): | |
$out = $_SERVER[$data]; break; | |
case (isset($_ENV[$data])): | |
$out = $_ENV[$data]; break; | |
case ($tmp = getenv($data)): | |
$out = $tmp; break; | |
case (function_exists('apache_getenv') && $tmp=apache_getenv($data, true)): | |
$out = $tmp; break; | |
default: | |
$out = false; | |
} | |
unset($tmp); | |
return $out; | |
}; | |
//Порядок условий зависит от приоритетов | |
switch(true){ | |
case ($tmp = $getEnv('HTTP_COMING_FROM')): | |
$out = $tmp; break; | |
case ($tmp = $getEnv('HTTP_X_COMING_FROM')): | |
$out = $tmp; break; | |
case ($tmp = $getEnv('HTTP_VIA')): | |
$out = $tmp; break; | |
case ($tmp = $getEnv('HTTP_FORWARDED')): | |
$out = $tmp; break; | |
case ($tmp = $getEnv('HTTP_FORWARDED_FOR')): | |
$out = $tmp; break; | |
case ($tmp = $getEnv('HTTP_X_FORWARDED')): | |
$out = $tmp; break; | |
case ($tmp = $getEnv('HTTP_X_FORWARDED_FOR')): | |
$out = $tmp; break; | |
case (!empty($_SERVER['REMOTE_ADDR'])): | |
$out=$_SERVER['REMOTE_ADDR']; break; | |
default: | |
$out = false; | |
} | |
unset($tmp); | |
return (false!==$out && preg_match('|^(?:[0-9]{1,3}\.){3,3}[0-9]{1,3}$|',$out, $matches)) ? $out : false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment