php实现多城市切换特效

前端之家收集整理的这篇文章主要介绍了php实现多城市切换特效前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们先来看下个人常用的代码
PHP;"> PHP

include_once('./qqwry.PHP');
$QQWry=new QQWry;

function get_real_ip(){
$ip=false;
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode (",",$_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip) { array_unshift($ips,$ip); $ip = FALSE; }
for ($i = 0; $i < count($ips); $i++) {
if (!eregi ("^(10|172.16|192.168).",$ips[$i])) {
$ip = $ips[$i];
break;
}
}
}
return $ip;
}

function is_ip($str) {
$ip = explode(".",$str);
if (count($ip)<4 || count($ip)>4) return 0;
foreach($ip as $ip_addr) {
if ( !is_numeric($ip_addr) ) return 0;
if ( $ip_addr<0 || $ip_addr>255 ) 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()+360024);
$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
<Meta charset="utf-8"/> 城市切换
    <LI class=i1><A href="index.php"&gt;全部小区</A></LI>

     <LI class=i1>
     <?php 
     echo $city; 
     ?>
     </LI>
     <LI class=i2 id=changeCity>切换城市</LI>
    </UL>
  @H_<a href="/tag/403/" target="_blank" class="keywords">403</a>_40@
@H_<a href="/tag/403/" target="_blank" class="keywords">403</a>_40@

@H_40340@
@H
403_40@

另外附上网友的实现方法
使用方法:解压后QQWry.Dat就是我们想要IP地址数据库,我们新建一个ipcity文件夹,将数据库放在下面。QQ IP数据库使用非常方便,数据也很齐全,你可以及时关注官方更新以保持数据最新,强力推荐一下:)
接下来,我们在上面的ipcity目录下新建一个ipaddress.PHP文件,直接复制以下代码进去即可,重要的地方也作了相应注释。B段:
PHP;"> 函数名称:ipCity 参数说明:$userip——用户IP地址 函数功能PHP通过IP地址判断用户所在城市 author:lee contact:xpsem2010@gmail.com */ function ipCity($userip) { //IP数据库路径,这里用的是QQ IP数据库 20110405 纯真版 $dat_path = 'QQWry.dat';

//判断IP地址是否有效
if(!ereg("^([0-9]{1,3}.){3}[0-9]{1,3}$",$userip)){
return 'IP Address Invalid';
}

//打开IP数据库
if(!$fd = @fopen($dat_path,'rb')){
return 'IP data file not exists or access denied';
}

//explode函数分解IP地址,运算得出整数形结果
$userip = explode('.',$userip);
$useripNum = $userip[0] 16777216 + $userip[1] 65536 + $userip[2] * 256 + $userip[3];

//获取IP地址索引开始和结束位置
$DataBegin = fread($fd,4);
$DataEnd = fread($fd,4);
$useripbegin = implode('',unpack('L',$DataBegin));
if($useripbegin < 0) $useripbegin += pow(2,32);
$useripend = implode('',$DataEnd));
if($useripend < 0) $useripend += pow(2,32);
$useripAllNum = ($useripend - $useripbegin) / 7 + 1;

$BeginNum = 0;
$EndNum = $useripAllNum;

//使用二分查找法从索引记录中搜索匹配的IP地址记录
while($userip1num>$useripNum || $userip2num<$useripNum) {
$Middle= intval(($EndNum + $BeginNum) / 2);

//偏移指针到索引位置读取4个字节
fseek($fd,$useripbegin + 7 * $Middle);
$useripData1 = fread($fd,4);
if(strlen($useripData1) < 4) {
  fclose($fd);
  return 'File Error';
}
//<a href="/tag/tiqu/" target="_blank" class="keywords">提取</a>出来的数据转换成长整形,如果数据是负数则<a href="/tag/jiashang/" target="_blank" class="keywords">加上</a>2的32次幂
$userip1num = implode('',$useripData1));
if($userip1num < 0) $userip1num += pow(2,32);



//提取的长整型数大于我们IP地址则修改结束位置进行下一次循环
if($userip1num > $useripNum) {
  $EndNum = $Middle;
  continue;
}



//取完上一个索引后取下一个索引
$DataSeek = fread($fd,3);
if(strlen($DataSeek) < 3) {
  fclose($fd);
  return 'File Error';
}
$DataSeek = implode('',$DataSeek.chr(0)));
fseek($fd,$DataSeek);
$useripData2 = fread($fd,4);
if(strlen($useripData2) < 4) {
  fclose($fd);
  return 'File Error';
}
$userip2num = implode('',$useripData2));
if($userip2num < 0) $userip2num += pow(2,32);



//找不到IP地址对应城市
if($userip2num < $useripNum) {
  if($Middle == $BeginNum) {
    fclose($fd);
    return 'No Data';
  }
  $BeginNum = $Middle;
}

}

$useripFlag = fread($fd,1);
if($useripFlag == chr(1)) {
$useripSeek = fread($fd,3);
if(strlen($useripSeek) < 3) {
fclose($fd);
return 'System Error';
}
$useripSeek = implode('',$useripSeek.chr(0)));
fseek($fd,$useripSeek);
$useripFlag = fread($fd,1);
}

if($useripFlag == chr(2)) {
$AddrSeek = fread($fd,3);
if(strlen($AddrSeek) < 3) {
fclose($fd);
return 'System Error';
}
$useripFlag = fread($fd,1);
if($useripFlag == chr(2)) {
$AddrSeek2 = fread($fd,3);
if(strlen($AddrSeek2) < 3) {
fclose($fd);
return 'System Error';
}
$AddrSeek2 = implode('',$AddrSeek2.chr(0)));
fseek($fd,$AddrSeek2);
} else {
fseek($fd,-1,SEEK_CUR);
}

while(($char = fread($fd,1)) != chr(0))
  $useripAddr2 .= $char;



$AddrSeek = implode('',$AddrSeek.chr(0)));
fseek($fd,$AddrSeek);



while(($char = fread($fd,1)) != chr(0))
  $useripAddr1 .= $char;

} else {
fseek($fd,SEEK_CUR);
while(($char = fread($fd,1)) != chr(0))
$useripAddr1 .= $char;

$useripFlag = fread($fd,SEEK_CUR);
}
while(($char = fread($fd,1)) != chr(0)){
  $useripAddr2 .= $char;
}

}
fclose($fd);

//返回IP地址对应的城市结果
if(preg_match('/http/i',$useripAddr2)) {
$useripAddr2 = '';
}
$useripaddr = "$useripAddr1 $useripAddr2";
$useripaddr = preg_replace('/CZ88.Net/is','',$useripaddr);
$useripaddr = preg_replace('/^s/is',$useripaddr);
$useripaddr = preg_replace('/s
$/is',$useripaddr);
if(preg_match('/http/i',$useripaddr) || $useripaddr == '') {
$useripaddr = 'No Data';
}

return $useripaddr;
}
?>

PHP根据IP地址实现城市切换或跳转
到这里,其实问题已经很简单了,用简单的js就通通搞定。C段如下:
跳转指定页面js取得城市 var city='';

//根据IP地址所有城市跳转指定页面
if(city.indexOf("上海市")>=0){
window.location.href="http://shanghai.demo.com/";
}

将开头的A段代码和上面的C段代码分别放在B段代码的头和尾,然后我们在需要跳转页面加入以下代码
刷新页面,是不是达到预想的效果了呢?
以上就是PHP获取IP地址、PHP根据IP地址判断城市以及PHP根据IP地址实现城市切换或跳转的详细介绍了,事实上,像PHP中通过IP地址自动切换城市就是这个方法的典型应用。举一反三,我们通过这种方法,也可以实现屏蔽指定IP地址或城市的用户访问,自己研究一下吧,也挺简单的:)

猜你在找的PHP相关文章