下面的代码是dedecms首页实现php代码显示访问者ip地址代码在需,要的位置放以下代码:
{dede:php}$user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"]; $user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"]; function get_real_ip(){ $ip=false; if(!empty($_SERVER["HTTP_CLIENT_IP"])){ $ip = $_SERVER["HTTP_CLIENT_IP"]; } if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']); if ($ip) { array_unshift($ips, $ip); $ip = FALSE; } for ($i = 0; $i < count($ips); $i++) { if (!eregi ("^(10│172.16│192.168).", $ips[$i])) { $ip = $ips[$i]; break; } } } return ($ip ? $ip : $_SERVER['REMOTE_ADDR']); } echo "YOUR IP "." ". get_real_ip();{/dede:php}
首页即可显示访问者的IP。
当然上面是专供织梦CMS使用的,在其他地方我们完全可以直接使用PHP代码来实现。
打印客户端IP:echo $_SERVER[‘REMOTE_ADDR’]; 或者: getenv('REMOTE_ADDR');
打印服务器IP:echo gethostbyname("www.vevb.com")
应用扩展:php代码判断地区省市或者ip段,IP地址获取所在城市代码如下:
<?phpfunction getClientIp($type = 0,$adv=false) { $type = $type?1:0; static $ip = NULL; if($ip !== NULL) return $ip[$type]; if($adv){ if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); $pos = array_search('unknown',$arr); if(false !== $pos) unset($arr[$pos]); $ip = trim($arr[0]); }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; }elseif (isset($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR']; } }elseif (isset($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR']; } $long = sprintf("%u",ip2long($ip)); $ip = $long ? array($ip, $long) : array('0.0.0.0', 0); return $ip[$type];}//获取省function getIpLocal($ip){ if($ip==""||$ip==null) $ip=getClientIp(); $url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=".$ip; $json = json_decode(file_get_contents($url)); $city=$json->city; $city=str_replace('市','',$city); $city = iconv("utf-8","gb2312",$city);//字符串转码 $province=$json->province; $province=str_replace('省','',$province); $province = iconv("utf-8","gb2312",$province);//字符串转码 return $province; }//获取市function getIpLocal_city($ip){ if($ip==""||$ip==null) $ip=getClientIp(); $url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=".$ip; $json = json_decode(file_get_contents($url)); $city=$json->city; $city=str_replace('市','',$city); $city = iconv("utf-8","gb2312",$city);//字符串转码 $province=$json->province; $province=str_replace('省','',$province); $province = iconv("utf-8","gb2312",$province);//字符串转码 return $city; }/*$diqu=getIpLocal("14.215.177.37");*///获取省$diqu=getIpLocal(getClientIp());//获取市$diqu_city=getIpLocal_city(getClientIp());//判断省if($diqu=='北京'||$diqu=='江苏'){ echo "window.location.href='http://www.vevb.com';";}//判断市if($diqu_city=='北京'){ echo "window.location.href='http://www.vevb.com';";}//判断ip段if($ip==""||$ip==null){ $ip=getClientIp(); } $arrayip = array('180.76.14.*','180.97.79.*','60.194.2.*');//ip段 $ipregexp = implode('|', str_replace( array('*','.'), array('/d+','/.') ,$arrayip) ); if(preg_match("/^(".$ipregexp.")$/", $ip)==1){ echo "window.location.href='http://www.vevb.com';"; }?>
上面的代码是基于新浪的开放API IP库实现。对一些想根据不同IP显示不同的内容的站点有参考行作用,大家可以借鉴参考。如果你不会或者不想自己处理,可以联系361源码在线客服帮你处理。
以上就是dedecms首页实现php代码显示访问者ip地址代码的全部内容,希望对大家的学习和解决疑问有所帮助,也希望大家多多支持武林网。新闻热点
疑难解答