PHP判断来访国内国外IP
Posted by he; tagged with php , ip , php判断
1. Javascript判断来访者的浏览器语言,如果是中文系统,自然使用者都是中国人,跳中文网站;如果是非中文系统,默认使用者非中国人,跳英文网站。
优点:判断反映速度快。
缺点:不准确,有可能中国用户喜欢用英文版系统,或者外国人使用中文系统的情况。
<--script type="text/javascript" language="javascript"> var Browser_Agent = navigator.userAgent; //浏览器为ie的情况 if(Browser_Agent.indexOf("MSIE")!=-1){ var a=navigator.browserLanguage; if(a !="zh-cn"){ location.href="英文网站"; document.write("国外IP"); } else document.write("国内IP"); } //浏览器非ie的情况 else{ var b=navigator.language; if(b!="zh-CN"){ location.href="英文网站"; document.write("国外IP"); } else document.write("国内IP"); } <--/script>
2.使用IP库来进行来访IP的判断
优点:判断准确。
缺点:响应速度没Javascript快。
需要引用一个PHP的IP库:下载附件ip.zip
我在网站头部引用jquery进行判断
<--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('国内IP'); } else { alert('国外IP'); //location.href="英文网站"; } }, complete:function(XMLHttpRequest,textStatus) { }, error:function() { } }); } <--/script> 《body onload="initurl()"> 《body>
checkip.php
setup($userip)) { echo 1; } else { echo 2; } ?>