我在网站头部引用jquery进行判断
<script type="text/javascript" src="/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript">
function initurl() {
$.ajax({
type:"GET",
url:"checkip.php",
dataType:"html",
data:"&time="+new Date(),
cache: false,
async: false,
beforeSend:function(XMLHttpRequest) {
},
success:function(msg) {
//如果返回值为1表示访问者为中国地区的ip
if(msg == 1){
//alert('I am China ip')
}
else {
//alert('I am not China ip')
location.href="英文网站"
}
},
complete:function(XMLHttpRequest,textStatus) {
},
error:function() {
}
})
}
</script>
<body onload="initurl()">
...
</body>
checkip.php页面的代码
$userip=$_SERVER['REMOTE_ADDR']
//引用ip库的文件 把ip.zip里的全部文件放在lib目录下
include_once('/lib/iplimit.class.php')
$iplimit = new iplimit
if($iplimit->setup($userip))
{
echo 1
}
else
{
echo 2
}
//返回ip所在的区域 外国ip精确到国名function getcposition($ip){
try {
$res1 = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$ip")
$res1 = json_decode($res1,true)
if ($res1[ "code"]==0){
return $res1['data']["country"].$res1['data'][ "region"].$res1['data']["city"]."_".$res1['data'][ "isp"]
}else{
return "未能获取"
}
}catch (Exception $e){
return "未能获取"
}
}
PHP服务器变量(环境变量)中的客户端IP地址:炒一个给你:
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP']
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']
} else {
$ip = $_SERVER['REMOTE_ADDR']
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)