dedecms首页添加根据IP访问区域跳转对应页面的方法
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>本文实例讲述了dedecms首页添加根据IP访问区域跳转对应页面的方法。分享给大家供大家参考。具体实现方法如下:</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
根据访问者的IP来判断所在区域,根据区域信息,来跳转相对应的页面,好多分类信息网,团购网都有这样的功能,根据你在的城市,然后跳转相应的页面显示此城市的相关信息.</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
先说下思路,首先获取访问者的IP信息,然后根据IP区域信息库进行对比,IP区域信息库网上有现成的,可下载到本地进行对比,也可直接用新浪或者淘宝,腾讯的API,这个自己baidu,注意编码问题,有的站点喜欢用GBK编码,有的喜欢用UTF-8编码,我用的淘宝的API,是UTF-8编码,对比后跳转到相对应的页面即可.</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
代码如下:<br>
</p>
<div class="msgheader" style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div class="right">
<span><u>复制代码</u></span>
</div>
代码如下:</div>
<div class="msgborder" id="phpcode1" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
$ip = getIp();//dede自带的查询IP功能,直接调用 <br>
$res1 = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$ip"); <br>
$res1 = json_decode($res1); <br>
$useraddre = $res1->data->region; <br>
$useraddre = mb_convert_encoding($useraddre, "GBK", "UTF-8"); <br>
$dzcity = "上海"; <br>
$contra= strstr($useraddre,$dzcity); <br>
if(!emptyempty($contra)){ <br>
//上海的,跳转到这里 <br>
header("Pragma: no-cache"); <br>
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); <br>
header('HTTP/1.1 301 Moved Permanently'); <br>
header("Location: http://www.jb51.net/"); <br>
exit;<br>
}</div>
<span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'> </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>这样写功能是能实现,但是要加在首页上面,也不知道效率如何,现在先这么写了.</span>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
我这里要判断的区域就上海的,所以就这么写了,如果还要判断其他地方的,自己再加判断就可以了.</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
希望本文所述对大家的dedecms建站有所帮助。</p>
頁:
[1]