/** * 获得用户的真实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函数,以后说不定能用上哦。
1 <?php 2 class common { 3 /* 4 5 * 获取服务器的时区 6 7 */ 8 9 function server_timezone(){ 10 11 if(function_exists('date_default_timezone_get')){ 12 13 return date_default_timezone_get(); 14 15 }else{ 16 17 return date('Z')/3600; 18 19 } 20 21 } 22 23 /* 24 25 * 获取字符串的长度 26 27 */ 28 29 function str_len($str){ 30 31 $length = strlen(preg_replace('/[x00-x7F]/','',$str)); 32 33 if($length){ 34 35 return strlen($str)-$length + intval($length/3)*2; 36 37 }else{ 38 39 return strlen($str); 40 41 } 42 43 } 44 45 /* 46 47 * 截取字符串 48 49 * $str 将要截取的字符换 50 51 * $length 截取字符串的长度 52 53 * $append 是否添加省落号 默认是 54 55 */ 56 57 public function sub_str($str,$length=0,$append = true){ 58 59 $str = trim($str); 60 61 $strlength = strlen($str); 62 63 if($length == 0 || $length >= $strlength){ 64 65 return $str; 66 67 }elseif( $length < 0){ 68 69 $length = $length + $strlength; 70 71 if($length < 0){ 72 73 $length = $strlength; 74 75 } 76 77 } 78 79 if(function_exists("mb_substr")){ 80 81 $newstr = mb_substr($str,0,$length,"gb2312"); 82 83 }elseif (function_exists('iconv_substr')){ 84 85 $newstr = iconv_substr($str, 0, $length, "gb2312"); 86 87 }else 88 89 { 90 91 $newstr = substr($str, 0, $length); 92 93 } 94 95 if ($append && $str != $newstr){ 96 97 $newstr .= '...'; 98 99 } 100 101 return $newstr; 102 103 } 104 105 /*获得当前格林威治时间的时间戳 106 107 * @return integer 108 109 */ 110 111 function getTime(){ 112 113 return (time() - date("Z")); 114 115 } 116 117 /* 118 119 * 创建像这样的查询条件 in('',''); 120 121 */ 122 123 function db_create_in($itme_list,$feild_name){ 124 125 if(empty($itme_list)){ 126 127 return $feild_name."IN('')"; 128 129 }else{ 130 131 if(!is_array($feild_name)){ 132 133 $itme_list = explode(",",$itme_list); 134 135 } 136 137 $itme_list = array_unique($itme_list); 138 139 $itme_list_tmp =""; 140 141 foreach($itme_list as $item){ 142 143 if(!$item($item)){ 144 145 $itme_list_tmp .= $itme_list_tmp?",'$item'":"'$item'"; 146 147 } 148 149 } 150 151 if(empty($itme_list_tmp)){ 152 153 return $feild_name."IN('')"; 154 155 }else{ 156 157 return $feild_name."IN('.$itme_list_tmp.')"; 158 159 } 160 161 } 162 } 163 164 /* 165 166 * 检查时间的格式是否正确 167 168 */ 169 170 function is_time($time){ 171 172 $pattern = '/[d]{4}-[d]{1,2}-[d]{1,2}s[d]{1,2}:[d]{1,2}:[d]{1,2}/'; 173 174 return preg_match($pattern,$time); 175 176 } 177 178 } 179 180 ?>