Home  >  Article  >  php教程  >  php实现多城市切换特效

php实现多城市切换特效

WBOY
WBOYOriginal
2016-06-06 19:48:381594browse

本文给大家主要分享的是在PHP中如何获取用户IP地址、PHP根据IP判断用户所在城市以及PHP根据IP实现城市切换或跳转的问题。

我们先来看下个人常用的代码

PHP

4) return 0; foreach($ip as $ip_addr) { if ( !is_numeric($ip_addr) ) return 0; if ( $ip_addr255 ) return 0; } return 1; } $ip=$_SERVER['REMOTE_ADDR']; $ifErr=$QQWry->QQWry($ip); $city = $QQWry->Country.$QQWry->Local; //echo $city;*/ if (strpos($city,'玄武区')!==false){ $city = "玄武区"; } else if(strpos($city,'仙林')!==false){ $city = "仙林"; } else if(strpos($city,'秦淮区')!==false){ $city = "秦淮区"; } else if(strpos($city,'江宁')!==false){ $city = "江宁"; } else if(strpos($city,'鼓楼')!==false){ $city = "鼓楼"; } else{ $city = "栖霞"; } if (isset($_GET['chengshi'])) { //Ê×Ïȼì²âÊÇ·ñ»ñµÃÁË GET ´«µÝµÄÊý¾Ý setcookie("cookie_city",$_GET['chengshi'],time()+3600*24); $city = $_GET['chengshi']; } else if (isset($_COOKIE["cookie_city"])) { //¼ì²âÊÇ·ñ´æÔÚêdzÆCookie $city = $_COOKIE["cookie_city"]; //ÏÔʾCookie±£´æµÄÊý¾Ý } else { setcookie("cookie_city",$city,time()+3600*24); //´¿³õʼ»¯´Ë±äÁ¿ $_COOKIE["cookie_city"] = $city; } ?>

HTML

城市切换

另外附上网友的实现方法:

使用方法:解压后QQWry.Dat就是我们想要IP地址数据库,我们新建一个ipcity文件夹,将数据库放在下面。QQ IP数据库使用非常方便,数据也很齐全,你可以及时关注官方更新以保持数据最新,强力推荐一下:)

接下来,我们在上面的ipcity目录下新建一个ipaddress.php文件,直接复制以下代码进去即可,重要的地方也作了相应注释。B段:

$useripNum || $userip2num $useripNum) { $EndNum = $Middle; continue; } //取完上一个索引后取下一个索引 $DataSeek = fread($fd, 3); if(strlen($DataSeek)

PHP根据IP地址实现城市切换或跳转

到这里,其实问题已经很简单了,用简单的js就通通搞定。C段如下:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn