public static function getClientIp()
{
//验证HTTP头中是否有REMOTE_ADDR
if (!isset($_SERVER['REMOTE_ADDR'])) { return '127.0.0.1'; } $clientIp = $_SERVER['REMOTE_ADDR'];
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])
&& preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
foreach ($matches[0] as $xip) {
if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
$clientIp = $xip;
break;
}
}
} elseif (isset($_SERVER['HTTP_CLIENT_IP']) && filter_var($clientIp, FILTER_VALIDATE_IP)) {
$clientIp = $_SERVER['HTTP_CLIENT_IP'];
}
return $clientIp;
}
public static function multisort(&$arr, $key, int $sort = SORT_ASC)
{
if (empty($arr)) {
return $arr;
}
if (count($arr) == count($arr, 1)) {
switch ($sort) {
case SORT_ASC:
sort($arr);
break;
case SORT_DESC:
rsort($arr);
break;
}
} else {
// 多维数组
$kArr = array_column($arr, $key);
if (!empty($kArr)) {
array_multisort($kArr, $sort, $arr);
}
}
}
|