php实现多城市切换特效

  • 2022-07-21 21:21:07
我们先来看下个人常用的代码
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()+3600*24);
$city = $_get['chengshi']; 
} 
else if (isset($_cookie["cookie_city"])) { //¼ì²âêç·ñ´æôúêç³æcookie  
$city = $_cookie["cookie_city"]; //ïôê¾cookie±£´æµäêý¾ý 
} 
else {  
setcookie("cookie_city",$city,time()+3600*24); //´¿³õê¼»¯´ë±äá¿ 
$_cookie["cookie_city"] = $city;
} 
?>
html
<!doctype html>
<html lang="zh-cn">
<head>
<meta charset="utf-8"/>
<link href="css/css.css" type=text/css rel=stylesheet>
<script src="js/jquery-1.4.2.min.js" type=text/javascript></script>
<title>城市切换</title>
</head>
<body>
<div class=selcity id=allcity style="display: none">
<div class=area>
<table>
 <tbody>
 <tr>
  <td><a class=cur href="show.html">玄武区</a></td>
  <td><a href="show1.html">仙林</a></td>
  <td><a href="show2.html">秦淮区</a></td>
  <td><a href="show3.html">鼓楼</a></td>
  <td><a href="show4.html">江宁</a></td>
  <td><a href="show5.html">栖霞区</a></td>
 </tr>
 </tbody></table>
<div class=none><a id=foldin href="javascript:;">全部</a></div></div></div>
<div class=header>
  <div class=area>
    <div class=r>
      <div class="topctiy clear">
        <ul>
         
        <li class=i1><a href="index.php">全部小区</a></li>
         
         <li class=i1>
         <?php 
         echo $city; 
         ?>
         </li>
         <li class=i2 id=changecity>切换城市</li>
        </ul>
      </div>
    </div>
  </div>
</div>
<script type=text/javascript>
jquery(function(){
  $("#changecity").click(function(a){
    $("#allcity").slidedown(300);
    a.stoppropagation();
    $(this).blur();
  });
  $("#allcity").click(function(a){
    a.stoppropagation()
  });
  $(document).click(function(a){
    a.button!=2 && $("#allcity").slideup(300)
  });
  $("#foldin").click(function(){
    $("#allcity").slideup(300)
  });
});
</script>
</body>
</html>
另外附上网友的实现方法:
使用方法:解压后qqwry.dat就是我们想要ip地址数据库,我们新建一个ipcity文件夹,将数据库放在下面。qq ip数据库使用非常方便,数据也很齐全,你可以及时关注官方更新以保持数据最新,强力推荐一下:)
接下来,我们在上面的ipcity目录下新建一个ipaddress.php文件,直接复制以下代码进去即可,重要的地方也作了相应注释。b段:
<?
/*
函数名称: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('', unpack('l', $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';
    }
    //提取出来的数据转换成长整形,如果数据是负数则加上2的32次幂
    $userip1num = implode('', unpack('l', $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('', unpack('l', $dataseek.chr(0)));
    fseek($fd, $dataseek);
    $useripdata2 = fread($fd, 4);
    if(strlen($useripdata2) < 4) {
      fclose($fd);
      return 'file error';
    }
    $userip2num = implode('', unpack('l', $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('', unpack('l', $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('', unpack('l', $addrseek2.chr(0)));
      fseek($fd, $addrseek2);
    } else {
      fseek($fd, -1, seek_cur);
    }



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



    $addrseek = implode('', unpack('l', $addrseek.chr(0)));
    fseek($fd, $addrseek);



    while(($char = fread($fd, 1)) != chr(0))
      $useripaddr1 .= $char;
  } else {
    fseek($fd, -1, seek_cur);
    while(($char = fread($fd, 1)) != chr(0))
      $useripaddr1 .= $char;



    $useripflag = fread($fd, 1);
    if($useripflag == chr(2)) {
      $addrseek2 = fread($fd, 3);
      if(strlen($addrseek2) < 3) {
        fclose($fd);
        return 'system error';
      }
      $addrseek2 = implode('', unpack('l', $addrseek2.chr(0)));
      fseek($fd, $addrseek2);
    } else {
      fseek($fd, -1, 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段如下:
//根据ip地址跳转指定页面js取得城市
var city='<?echo ipcity($xp_userip);?>';

//根据ip地址所有城市跳转到指定页面
if(city.indexof("上海市")>=0){
    window.location.href="http://shanghai.demo.com/";
}
将开头的a段代码和上面的c段代码分别放在b段代码的头和尾,然后我们在需要跳转的页面加入以下代码:
<script src="/ipcity/ipaddress.php" type="text/javascript" language="javascript"></script>
刷新页面,是不是达到预想的效果了呢?
以上就是php获取ip地址、php根据ip地址判断城市以及php根据ip地址实现城市切换或跳转的详细介绍了,事实上,像php中通过ip地址自动切换城市就是这个方法的典型应用。举一反三,我们通过这种方法,也可以实现屏蔽指定ip地址或城市的用户访问,自己研究一下吧,也挺简单的:)

猜你喜欢