最近做了公司网站的一个需求:按来访人的IP来判断他是进中文网站还是英文网站。
我大概构思了一下,有两个方案:
1. Javascript判断来访者的浏览器语言,如果是中文系统,自然使用者都是中国人,跳中文网站;
如果是非中文系统,默认使用者非中国人,跳英文网站。
优点:判断反映速度快。
缺点:不准确,有可能中国用户喜欢用英文版系统,或者外国人使用中文系统的情况。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<
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 = " 英文网站 " ;
}
}
// 浏览器非ie的情况
else {
var b = navigator.language;
if (b != " zh-CN " ){
location.href = " 英文网站 " ;
}
}
< / script>
var Browser_Agent = navigator.userAgent;
// 浏览器为ie的情况
if (Browser_Agent.indexOf( " MSIE " ) !=- 1 ){
var a = navigator.browserLanguage;
if (a != " zh-cn " ){
location.href = " 英文网站 " ;
}
}
// 浏览器非ie的情况
else {
var b = navigator.language;
if (b != " zh-CN " ){
location.href = " 英文网站 " ;
}
}
< / script>
2.使用IP库来进行来访IP的判断
优点:判断准确。
缺点:响应速度没Javascript快。
需要引用一个PHP的IP库:/Files/tianxin2001x/ip.zip
我在网站头部引用jquery进行判断
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<
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 >
< 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.zip里的全部文件放在lib目录下
include_once ( ' /lib/iplimit.class.php ' );
$iplimit = new iplimit;
if ( $iplimit -> setup( $userip ))
{
echo 1 ;
}
else
{
echo 2 ;
}
两种方法都可以完美实现判断来访IP,选择哪种就看你的具体需求了。