PHP implementation of multi-city switching special effects _php skills

Source: Internet
Author: User
Tags explode fread pow strlen unpack
Let's take a look at the code that's used personally.
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|19
2\.168) \. ", $ips [$i])) {$ip = $ips [$i]; break;}}
return $IP;
  The 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, ' Xuanwu District ')!==false) {$city = "Xuanwu District";} else if (Strpos ($city, ' Fairy Forest ')!==false) {$city = "cents
Lin "; else if (Strpos ($city, ' Qinhuai District ')!==false) {$city = "Qinhuai District"; else if (Strpos ($city, ' Jiangning ')!==false) {$city = "Jiangning";} else if (Strpos ($city, ' Drum Tower ')!==false) {$city = "drum Tower";} else{$c
ity = "Qixia"; } if (Isset ($_get[' Chengshi ')) {//êxïè¼ì²âêç Ñ»ñµãáë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>  
Also attach the realization method of the Netizen:
How to use: After decompression QQWry.Dat is we want IP address database, we create a new ipcity folder, put the database below. QQ IP database is very convenient to use, the data is also very complete, you can timely attention to the official update to maintain the latest data, a strong recommendation:
Next, we in the above ipcity directory to create a new ipaddress.php file, directly copy the following code to go in, important place also made corresponding comments. Paragraph b:
? /* Function Name: ipCity parameter Description: $userip--User IP address function: PHP through IP address to determine the user's City author:lee contact:xpsem2010@gmail.com * * Function ipCity ($



  Userip) {//IP database path, here is the QQ IP database 20110405 Pure version $dat _path = ' QQWry.dat '; Determine if the IP address is valid if (!ereg ("^" ^ ([0-9]{1,3}.) {3} [0-9]
  {1,3}$ ", $userip)) {return ' IP address Invalid ';
  //Open IP database if (! $fd = @fopen ($dat _path, ' RB ')) {return ' IP data file not exists or access denied ';
  The//explode function decomposes the IP address, and the result of the integer shape is calculated $userip = Explode ('. ', $userip);



  $useripNum = $userip [0] * 16777216 + $userip [1] * 65536 + $userip [2] * 256 + $userip [3];
  Gets the IP address index start and end position $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; Search for matching I from index records using the binary lookup methodP Address record while ($userip 1num> $useripNum | | $userip 2num< $useripNum) {$Middle = Intval (($EndNum + $BeginNum)/2);
    The offset pointer reads 4 bytes to the index position fseek ($FD, $useripbegin + 7 * $Middle);
    $useripData 1 = fread ($FD, 4);
      if (strlen ($useripData 1) < 4) {fclose ($FD);
    Return ' File Error ';
    ///Extract Data conversion growth shaping, if the data is negative then add 2 of the 32 power $userip 1num = implode (', Unpack (' L ', $useripData 1));



    if ($userip 1num < 0) $userip 1num + + POW (2, 32);
      The number of long integers fetched is greater than our IP address. To modify the end position for the next loop if ($userip 1num > $useripNum) {$EndNum = $Middle;
    Continue
    After the previous index is taken, remove an index $DataSeek = fread ($FD, 3);
      if (strlen ($DataSeek) < 3) {fclose ($FD);
    Return ' File Error ';
    $DataSeek = Implode (', Unpack (' L ', $DataSeek. chr (0)));
    Fseek ($FD, $DataSeek);
    $useripData 2 = fread ($FD, 4);
      if (strlen ($useripData 2) < 4) {fclose ($FD);
    Return ' File Error ';
   $userip 2num = Implode (', Unpack (' L ', $useripData 2)); if ($userip 2num < 0) $userip 2num + + POW (2, 32);
        Could not find IP address corresponding city if ($userip 2num < $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)) {$AddrSeek 2 = fread ($FD, 3);
        if (strlen ($AddrSeek 2) < 3) {fclose ($FD);
      Return ' System Error ';
      $AddrSeek 2 = implode (', Unpack (' L ', $AddrSeek 2.chr (0)));
   Fseek ($FD, $AddrSeek 2); else {fseek ($FD,-1, seek_cur);



    while ($char = Fread ($FD, 1))!= chr (0)) $useripAddr 2. = $char;
    $AddrSeek = Implode (', Unpack (' L ', $AddrSeek. chr (0)));



    Fseek ($FD, $AddrSeek);
  while ($char = Fread ($FD, 1))!= chr (0)) $useripAddr 1. = $char;
    else {fseek ($FD,-1, seek_cur);



    while ($char = Fread ($FD, 1))!= chr (0)) $useripAddr 1. = $char;
    $useripFlag = Fread ($FD, 1);
      if ($useripFlag = = Chr (2)) {$AddrSeek 2 = fread ($FD, 3);
        if (strlen ($AddrSeek 2) < 3) {fclose ($FD);
      Return ' System Error ';
      $AddrSeek 2 = implode (', Unpack (' L ', $AddrSeek 2.chr (0)));
    Fseek ($FD, $AddrSeek 2);
    else {fseek ($FD,-1, seek_cur);
    while ($char = Fread ($FD, 1))!= chr (0)) {$useripAddr 2. = $char;



  } fclose ($FD);
  Returns the city result of the IP address if (preg_match ('/http/i ', $useripAddr 2)) {$useripAddr 2 = ';
  $USERIPADDR = "$useripAddr 1 $useripAddr 2"; $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 to achieve city switching or jump based on IP address
Here, in fact, the problem has been very simple, with simple JS will be all done. Paragraph C reads as follows:
Jump the specified page according to the IP address JS get the city
var city= ' <?echo ipCity ($xp _userip);? > ';

According to the IP address all cities jump to the specified page
if (City.indexof ("Shanghai") >=0) {
    window.location.href= "http://shanghai.demo.com/";
}
Place the first section A and the C section of code on the head and tail of code B, and then we add the following code to the page where we want to jump:
<script src= "/ipcity/ipaddress.php" type= "Text/javascript" language= "JavaScript" ></script>
Refresh the page, is not to achieve the desired effect?
The above is the PHP to obtain IP address, PHP based on IP address to judge the city and PHP based on IP address to achieve city switching or jump detailed introduction, in fact, like PHP through IP address automatic switching city is the typical application of this method. Extrapolate, in this way, we can also be implemented to mask the specified IP address or city user access, study it yourself, it is quite simple:)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.