In PHP the city is judged according to the IP address to realize city switching or jump code
- 2020-05-19 04:24:11
- OfStack
Get the IP address
Method 2
PHP determines the user's city based on the IP address
We have already obtained the IP address of the user above. Next, we will obtain the user's city according to this IP address. To get started, we need to download a ready-made QQ IP database.
Latest IP address database: / / www ofstack. com softs / 10529. html
How to use: after unzipping QQWry, Dat means that we want the IP address database. We will create a new ipcity folder and put the database at the bottom. QQ IP database is very easy to use, the data is complete, you can follow the official update to keep the data up to date, highly recommended 1 :)
Next, we will create a new ipaddress.php file in the ipcity directory above and just copy the following code into it. B section:
PHP realizes city switching or jump based on IP address
At this point, the problem is actually very simple, using a simple js to get it all done. The C paragraph reads:
// redirect to the specified page js to get the city according to the IP address
// jump to the specified page for all cities according to the IP address
Put the starting A code and the C code above at the beginning and end of the B code respectively, and then we add the following code to the page where we want to jump:
Refresh the page, is it achieve the desired effect?
The above code is just a simple judgment ip jump there, if such as, I want to put hunan all IP jump to changsha, that like hengyang, liu Yang, leiyang these will do many parent line processing, want to know look at the next set.
<?php
function GetIP() {
if ($_SERVER["HTTP_X_FORWARDED_FOR"])
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
else if ($_SERVER["HTTP_CLIENT_IP"])
$ip = $_SERVER["HTTP_CLIENT_IP"];
else if ($_SERVER["REMOTE_ADDR"])
$ip = $_SERVER["REMOTE_ADDR"];
else if (getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
else if (getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
else
$ip = "Unknown";
return $ip;
}
echo GetIP();
?>
Method 2
function getip ()
{
if (getenv('http_client_ip')) {
$ip = getenv('http_client_ip');
} else if (getenv('http_x_forwarded_for')) {
$ip = getenv('http_x_forwarded_for');
} else if (getenv('remote_addr')) {
$ip = getenv('remote_addr');
} else {
$ip = $_server['remote_addr'];
}
return $ip;
PHP determines the user's city based on the IP address
We have already obtained the IP address of the user above. Next, we will obtain the user's city according to this IP address. To get started, we need to download a ready-made QQ IP database.
Latest IP address database: / / www ofstack. com softs / 10529. html
How to use: after unzipping QQWry, Dat means that we want the IP address database. We will create a new ipcity folder and put the database at the bottom. QQ IP database is very easy to use, the data is complete, you can follow the official update to keep the data up to date, highly recommended 1 :)
Next, we will create a new ipaddress.php file in the ipcity directory above and just copy the following code into it. B section:
<?
/*
Function name: ipCity
Parameter description: $userip - the user IP address
Functions: PHP through IP The address determines the user's city
author:lee
contact:xpsem2010@gmail.com
*/
function ipCity($userip) {
//IP The database path, which I'm going to use here QQ IP The database 20110405 Pure version
$dat_path = 'QQWry.dat';
// judge IP Whether the address is valid or not
if(!ereg("^([0-9]{1,3}.){3}[0-9]{1,3}$", $userip)){
return 'IP Address Invalid';
}
// Open the IP The database
if(!$fd = @fopen($dat_path, 'rb')){
return 'IP data file not exists or access denied';
}
//explode The function decomposition IP Address, and the result is an integer
$userip = explode('.', $userip);
$useripNum = $userip[0] * 16777216 + $userip[1] * 65536 + $userip[2] * 256 + $userip[3];
// To obtain IP Address index start and end locations
$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;
// use 2 The sub-lookup method searches for matches from indexed records IP The geologic record
while($userip1num>$useripNum || $userip2num<$useripNum) {
$Middle= intval(($EndNum + $BeginNum) / 2);
// Offset pointer to index position read 4 bytes
fseek($fd, $useripbegin + 7 * $Middle);
$useripData1 = fread($fd, 4);
if(strlen($useripData1) < 4) {
fclose($fd);
return 'File Error';
}
// The extracted data is transformed into plastic, and if the data is negative, it is added 2 the 32 power
$userip1num = implode('', unpack('L', $useripData1));
if($userip1num < 0) $userip1num += pow(2, 32);
// The number of long integers extracted is larger than ours IP Address is modified to the end of the location under 1 loops
if($userip1num > $useripNum) {
$EndNum = $Middle;
continue;
}
// Take on the 1 Take down the index 1 index
$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);
// Can't find IP Address corresponding to city
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);
// return IP The address corresponds to the city result
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 realizes city switching or jump based on IP address
At this point, the problem is actually very simple, using a simple js to get it all done. The C paragraph reads:
// redirect to the specified page js to get the city according to the IP address
var city='<?echo ipCity($xp_UserIp);?>';
// jump to the specified page for all cities according to the IP address
if(city.indexOf(" Shanghai ")>=0){
window.location.href="http://shanghai.demo.com/";
}
Put the starting A code and the C code above at the beginning and end of the B code respectively, 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 it achieve the desired effect?
The above code is just a simple judgment ip jump there, if such as, I want to put hunan all IP jump to changsha, that like hengyang, liu Yang, leiyang these will do many parent line processing, want to know look at the next set.