/**
* 获得用户的真实IP地址
*
* @access public
* @return string
*/
function real_ip()
{
static $realip = NULL; if ($realip !== NULL)
{
return $realip;
} if (isset($_SERVER))
{
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
foreach ($arr AS $ip)
{
$ip = trim($ip); if ($ip != 'unknown')
{
$realip = $ip; break;
}
}
}
elseif (isset($_SERVER['HTTP_CLIENT_IP']))
{
$realip = $_SERVER['HTTP_CLIENT_IP'];
}
else
{
if (isset($_SERVER['REMOTE_ADDR']))
{
$realip = $_SERVER['REMOTE_ADDR'];
}
else
{
$realip = '0.0.0.0';
}
}
}
else
{
if (getenv('HTTP_X_FORWARDED_FOR'))
{
$realip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_CLIENT_IP'))
{
$realip = getenv('HTTP_CLIENT_IP');
}
else
{
$realip = getenv('REMOTE_ADDR');
}
} preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
$realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0'; return $realip;
}

如果大家还有别的方式的话,可以一起交流哦。

下面再给大家一些常用的PHP函数,以后说不定能用上哦。

 <?php
class common {
/* * 获取服务器的时区 */ function server_timezone(){ if(function_exists('date_default_timezone_get')){ return date_default_timezone_get(); }else{ return date('Z')/3600; } } /* * 获取字符串的长度 */ function str_len($str){ $length = strlen(preg_replace('/[\x00-\x7F]/','',$str)); if($length){ return strlen($str)-$length + intval($length/3)*2; }else{ return strlen($str); } } /* * 截取字符串 * $str 将要截取的字符换 * $length 截取字符串的长度 * $append 是否添加省落号 默认是 */ public function sub_str($str,$length=0,$append = true){ $str = trim($str); $strlength = strlen($str); if($length == 0 || $length >= $strlength){ return $str; }elseif( $length < 0){ $length = $length + $strlength; if($length < 0){ $length = $strlength; } } if(function_exists("mb_substr")){ $newstr = mb_substr($str,0,$length,"gb2312"); }elseif (function_exists('iconv_substr')){ $newstr = iconv_substr($str, 0, $length, "gb2312"); }else { $newstr = substr($str, 0, $length); } if ($append && $str != $newstr){ $newstr .= '...'; } return $newstr; } /*获得当前格林威治时间的时间戳 * @return integer */ function getTime(){ return (time() - date("Z")); } /* * 创建像这样的查询条件 in('',''); */ function db_create_in($itme_list,$feild_name){ if(empty($itme_list)){ return $feild_name."IN('')"; }else{ if(!is_array($feild_name)){ $itme_list = explode(",",$itme_list); } $itme_list = array_unique($itme_list); $itme_list_tmp =""; foreach($itme_list as $item){ if(!$item($item)){ $itme_list_tmp .= $itme_list_tmp?",'$item'":"'$item'"; } } if(empty($itme_list_tmp)){ return $feild_name."IN('')"; }else{ return $feild_name."IN('.$itme_list_tmp.')"; } }
} /* * 检查时间的格式是否正确 */ function is_time($time){ $pattern = '/[\d]{4}-[\d]{1,2}-[\d]{1,2}\s[\d]{1,2}:[\d]{1,2}:[\d]{1,2}/'; return preg_match($pattern,$time); } } ?>
05-11 14:49