获取本地IP地址
PHP Code复制内容到剪贴板
- //获取IP地址类型(0为自动,1为REMOTE_ADDR,2为HTTP_X_FORWARDED_FOR,3为HTTP_CLIENT_IP)
- $getiptype = 0;
- $pagechar = 'utf-8';
- //取得IP
- function egetip(){
- if(getenv('HTTP_CLIENT_IP')&&strcasecmp(getenv('HTTP_CLIENT_IP'),'unknown'))
- {
- $ip=getenv('HTTP_CLIENT_IP');
- }
- elseif(getenv('HTTP_X_FORWARDED_FOR')&&strcasecmp(getenv('HTTP_X_FORWARDED_FOR'),'unknown'))
- {
- $ip=getenv('HTTP_X_FORWARDED_FOR');
- }
- elseif(getenv('REMOTE_ADDR')&&strcasecmp(getenv('REMOTE_ADDR'),'unknown'))
- {
- $ip=getenv('REMOTE_ADDR');
- }
- elseif(isset($_SERVER['REMOTE_ADDR'])&&$_SERVER['REMOTE_ADDR']&&strcasecmp($_SERVER['REMOTE_ADDR'],'unknown'))
- {
- $ip=$_SERVER['REMOTE_ADDR'];
- }
- if($getiptype>0)
- {
- $ip=egetipadd();
- }
- $ip=RepPostVar(preg_replace("/^([\d\.]+).*/","\\1",$ip));
- return $ip;
- }
- //取得IP附加
- function egetipadd(){
- if($getiptype==2)
- {
- $ip=getenv('HTTP_X_FORWARDED_FOR');
- }
- elseif($getiptype==3)
- {
- $ip=getenv('HTTP_CLIENT_IP');
- }
- else
- {
- $ip=getenv('REMOTE_ADDR');
- }
- return $ip;
- }
- //参数处理函数
- function RepPostVar($val){
- if($val!=addslashes($val))
- {
- exit();
- }
- CkPostStrChar($val);
- $val=str_replace("%","",$val);
- $val=str_replace(" ","",$val);
- $val=str_replace("`","",$val);
- $val=str_replace("\t","",$val);
- $val=str_replace("%20","",$val);
- $val=str_replace("%27","",$val);
- $val=str_replace("*","",$val);
- $val=str_replace("'","",$val);
- $val=str_replace("\"","",$val);
- $val=str_replace("/","",$val);
- $val=str_replace(";","",$val);
- $val=str_replace("#","",$val);
- $val=str_replace("--","",$val);
- $val=RepPostStr($val,1);
- $val=addslashes($val);
- return $val;
- }
- //处理编码字符
- function CkPostStrChar($val){
- if(substr($val,-1)=="\\")
- {
- exit();
- }
- }
- //处理提交字符
- function RepPostStr($val,$ecms=0,$phck=0){
- if($phck==1)
- {
- CkPostStrCharYh($val);
- }
- $val=ehtmlspecialchars($val,ENT_QUOTES);
- if($ecms==0)
- {
- CkPostStrChar($val);
- $val=AddAddsData($val);
- }
- return $val;
- }
- //处理提交字符2
- function RepPostStr2($val,$phck=0){
- if($phck==1)
- {
- CkPostStrCharYh($val);
- }
- CkPostStrChar($val);
- $val=AddAddsData($val);
- return $val;
- }
- //增加adds
- function AddAddsData($data){
- if(!MAGIC_QUOTES_GPC)
- {
- $data=addslashes($data);
- }
- return $data;
- }
- //验证编码字符
- function CkPostStrCharYh($val){
- if($val!=addslashes($val))
- {
- exit();
- }
- return $val;
- }
- //htmlspecialchars处理
- function ehtmlspecialchars($val,$flags=ENT_COMPAT){
- if(PHP_VERSION>='5.4.0')
- {
- if($pagechar=='utf-8')
- {
- $char='UTF-8';
- }
- else
- {
- $char='ISO-8859-1';
- }
- $val=htmlspecialchars($val,$flags,$char);
- }
- else
- {
- $val=htmlspecialchars($val,$flags);
- }
- return $val;
- }
- echo egetip();
上一篇 PHPExcel
下一篇 php接收base64图片并保存