Php accesses domestic and foreign IP addresses to determine code and implement page Jump

Source: Internet
Author: User

I have come up with two solutions:
1. Javascript judges the visitor's browser language. If it is a Chinese system, all natural users are Chinese and skip Chinese websites;

If it is not a Chinese system, the default user is not Chinese and the English-speaking website is skipped.

Advantage: quick response.
Disadvantage: inaccurate. It is possible that Chinese users prefer the English version of the system or that foreigners use the Chinese version of the system.


Code
Copy codeThe Code is as follows:
<Script type = "text/javascript" language = "javascript">
Var Browser_Agent = navigator. userAgent;
// When the browser is ie
If (Browser_Agent.indexOf ("MSIE ")! =-1 ){
Var a = navigator. browserLanguage;
If (! = "Zh-cn "){
Location. href = "English website ";
}
}
// Non-ie browser
Else {
Var B = navigator. language;
If (B! = "Zh-CN "){
Location. href = "English website ";
}
}
</Script>


2. Use the IP address library to determine the access IP Address

Advantage: accurate judgment.
Disadvantage: the response speed is not Javascript fast.

You need to reference a php ip library ip_php.zip

I reference jquery in the website header for judgment.
Copy codeThe Code is as follows:
<Script type = "text/javascript" src = "/javascript/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 is the ip address of the China region.
If (msg = 1 ){
// Alert ('I am China ip ');
}
Else {
// Alert ('I am not China ');
Location. href = "English website ";
}
},
Complete: function (XMLHttpRequest, textStatus ){
},
Error: function (){
}
});
}
</Script>
<Body onload = "initurl ()">
...
</Body>

Code for the checkip. php page:
Copy codeThe Code is as follows:
$ Userip = $ _ SERVER ['remote _ ADDR '];
// Reference the ip library file to put all the files in ip.zip 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 used to determine the access IP address perfectly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.