PHP visit IP judge code and achieve page jump

  • 2020-03-31 20:04:51
  • OfStack

I had a rough idea, and there were two plans:

1. Javascript determines the browser language of the visitor. If it is a Chinese system, the users are all Chinese naturally.

If the system is not in Chinese, the default user is not Chinese, skip the English website.

Advantage: the judgment reaction speed is fast.
Disadvantages: inaccurate, it may be that Chinese users prefer to use the English version of the system, or foreigners use the Chinese version of the system.

code


<script type="text/javascript" language="javascript"> 
var Browser_Agent=navigator.userAgent; 
//The browser is ie
if(Browser_Agent.indexOf("MSIE")!=-1){ 
var a=navigator.browserLanguage; 
if(a !="zh-cn"){ 
location.href=" English website "; 
} 
} 
//The browser is not ie
else{ 
var b=navigator.language; 
if(b!="zh-CN"){ 
location.href=" English website "; 
} 
} 
</script>

2. Use the IP library to judge the visiting IP

Advantage: accurate judgment.
Cons: not as responsive as Javascript.
Need to refer to a PHP IP library (link: http://xiazai.jb51.net/200912/yuanma/ip_php.rar)
I refer to jquery in the header of the site


<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) { 
//If the return value is 1, the visitor's IP is in China
if(msg == 1){ 
//alert('I am China ip'); 
} 
else { 
//alert('I am not China ip'); 
location.href=" English website "; 
} 
}, 
complete:function(XMLHttpRequest,textStatus) { 
}, 
error:function() { 
} 
}); 
} 
</script> 
<body onload="initurl()"> 
... 
</body> 

Code for checkip.php page:


$userip=$_SERVER['REMOTE_ADDR']; 
//Files that reference the IP library put all the files in the ip.zip directory under the lib directory
include_once('/lib/iplimit.class.php'); 
$iplimit = new iplimit; 
if($iplimit->setup($userip)) 
{ 
echo 1; 
} 
else 
{ 
echo 2; 
} 

Both methods can be perfect to determine the visiting IP, which depends on your specific needs.


Related articles: