您好,登錄后才能下訂單哦!
利用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()+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
<!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 > <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怎么實現一個多城市切換特效問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。