Created
October 27, 2014 13:29
-
-
Save cnnewjohn/15d6cd9bbb6a02de6522 to your computer and use it in GitHub Desktop.
Helper.php 助手类,封装了常用方法
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 | |
/** | |
* Helper.php 助手类,封装了常用方法 | |
*/ | |
class Helper | |
{ | |
/** | |
* get_client_ip, 获取用户IP地址 | |
* | |
* @param long $to_long, 是否转为长整型,默认否 | |
*/ | |
public static function get_client_ip($to_long = FALSE) | |
{ | |
$keys = array('HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'REMOTE_ADDR'); | |
$ip = ''; | |
foreach ($keys as $key) { | |
if ($ip = Arr::get($_SERVER, $key, '')) break; | |
} | |
if (! $ip) { | |
foreach ($keys as $key) { | |
if ($ip = getenv($key)) break; | |
} | |
} | |
// 某些情况下会取得多个IP,如1.2.3.4, 1.2.3.5,取第一个ip | |
$ip = Arr::get(explode(', ', $ip), 0); | |
return $to_long ? self::ip2long($ip) : $ip; | |
} | |
/** | |
* ip2long, 解决原生ip2long的负数问题 | |
* | |
* @param string $ip | |
* @return long | |
*/ | |
public static function ip2long($ip) | |
{ | |
return bindec(decbin(ip2long($ip))); | |
} | |
/** | |
* _exec, 执行系统命令,并返回处理后的结果 | |
*/ | |
public static function exec($cmd) | |
{ | |
echo "{$cmd}\r\n"; | |
exec($cmd, $out, $status); | |
$out = implode("\r\n", $out); | |
return $out; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment