Put it in index.php.
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
include _ once ' class _ I location . PHP ';
$ IP = ' 222. 132.78 . 170 ';
$ iplo = new I location();
$a_provinces=Array(
0' = >' Hebei',
1 ' = >; Hainan',
2 ' = >;' Guizhou',
3 ' = >; Guangxi',
4 ' = >;' Guangdong',
5 ' = >;' Gansu',
6 ' = >; Fujian',
7 ' = >; Beijing',
8 ' = >;' Anhui',
9 ' = >;' Henan',
10 ' = >; Heilongjiang',
1 1 ' = >;' Hubei',
12' = > Hunan',
13 ' = >; Jilin',
14 ' = >; Jiangsu',
15 ' = >; Jiangxi',
16 ' = >;' Liaoning',
17' = > Inner Mongolia',
18 ' = >; Ningxia',
19' = > Qinghai',
20 ' = >; Shandong',
2 1 ' = >; Shanxi',
22 ' = >; Shaanxi',
23 ' = >; Shanghai',
24 ' = >;' Sichuan',
25 ' = >;' Tianjin',
26 ' = >; Tibet',
27 ' = >; Xinjiang',
28 ' = >; Yunnan',
29 ' = >; Zhejiang',
30 ' = >;' Chongqing',
3 1 ' = >; Hong Kong',
32 ' = >; Macau',
33 ' = >; Taiwan Province Province
);
$ China =“”;
$ strs = $ iplo-& gt; get location($ IP);
//print _ r($ strs);
//echo $ strs[' country '];
foreach($a _ provinces as$ci)
{
$pos = strpos($strs['country'],$ ci);
if ($pos! = = False)
echo $ ci
$ china = $ ci
Break;
}
}
Echo $ China;
If ($ China! ='')
$ home = ' Chinese/index . PHP ';
other
$ home = ' English/index . PHP ';
Echo "< scripting language =' JavaScript'> window. location.href ='". $home。 ”; & lt/script & gt; " ;
& gt
Description: 1.qqwry.dat can be found in the qq folder. These three files should be placed in the same directory.
2. Class file: class _ iplocation.php
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
/**
* IP geographic location query class
*
* @ Author Ma
* @ Version 1.5
* @ Copyright 2005 CoolCode.CN.
*/
Category I location {
/**
* QQWry。 Dat file pointer
*
* @var resources
*/
var $ fp
/**
* the offset address of the first IP record
*
* @var int
*/
var $ firstip
/**
* Offset address of the last IP record
*
* @var int
*/
var $ lastip
/**
* The total number of IP records (excluding version information records)
*
* @var int
*/
var $ totalip
/**
* Returns the read long integer.
*
* @ Access Private
* @return int
*/
Function getlong() {
//Convert the read 4-byte little-endian code into a long integer.
$result = unpack('Vlong ',fread($ this-& gt; fp,4));
Returns $ result ['long'];
}
/**
* Returns the read 3-byte integer.
*
* @ Access Private
* @return int
*/
Function getlong3() {
//Convert the read 3-byte little-endian code into a long integer.
$result = unpack('Vlong ',fread($ this-& gt; fp,3)。 chr(0));
Returns $ result ['long'];
}
/**
* return the IP address that can be compared after compression.
*
* @ Access Private
* @param string $ip
* @ Returns a string
*/
Function package (ip)
//Converts the IP address into a long integer, and returns False if the IP address in PHP5 is wrong.
//At this time, intval converts Flase into an integer-1, and then compresses it into a string encoded by big-endian.
return pack('N ',intval(IP 2 long($ IP)));
}
/**
* Returns the read string.
*
* @ Access Private
* @param string $data
* @ Returns a string
*/
Function getstring ($ data = "") (
$ char = fread($ this-& gt; fp, 1);
While(order($ char)>0) {// The string is saved in C format and ends with \0.
$data。 = $ char// Concatenates the read characters after the given string.
$ char = fread($ this-& gt; fp, 1);
}
Return to $ data
}
/**
* Return regional information
*
* @ Access Private
* @ Returns a string
*/
Function getarea() {
$ byte = fread($ this-& gt; fp, 1); //Flag byte
switch(order($ byte)){
Case 0: // No regional information.
$ area =
Break;
Case 1:
Case 2://The flag byte is 1 or 2, indicating that the area information is redirected.
fseek($ this-& gt; fp,$ this-& gt; getlong 3());
$ area = $ this-& gt; getstring();
Break;
Default: // Otherwise, it means that the area information has not been redirected.
$ area = $ this-& gt; getstring($ byte);
Break;
}
return $ area
}
/**
* according to the given IP address or domain name, return the regional information.
*
* @ Access to the public
* @param string $ip
* @ Returns an array
*/
Function getlocation($ip) {
If (! $ this-& gt; Fp) returns null// If the data file is not opened correctly, it returns null directly.
$ location[' IP ']= gethostbyname($ IP); //Convert the entered domain name into an IP address.
$ IP = $ this-& gt; pack IP($ location[' IP ']); //Convert the input IP address into a comparable IP address.
//Illegal IP address will be converted to 255.255.255.255.
//method of bisection
$ l = 0; //Lower limit of search
$ u = $ this-& gt; Totalip// Upper limit of search
$ findip = $ this-& gt; Last//If it is not found, return the version information of the last IP record (QQWry). Dat)。
while($l & lt; = $u) {// When the upper boundary is smaller than the lower boundary, the search fails.
$ I = floor(($ l+$ u)/2); //Calculate approximate intermediate records
fseek($ this-& gt; fp,$ this-& gt; first IP+$ I * 7);
$ beginip = strrev(fread($ this-& gt; fp,4)); //Get the starting IP address of the intermediate record.
//The function of strrev here is to convert the compressed IP address of little-endian into the format of big-endian.
//For comparison, the same goes for the back.
if($ IP & lt; $beginip) {// When the user's ip is less than the initial IP address of the intermediate record,
$ u = $ I- 1; //Modify the upper limit of search to intermediate record minus one.
}
Otherwise {
fseek($ this-& gt; fp,$ this-& gt; getlong 3());
$ endip = strrev(fread($ this-& gt; fp,4)); //Get the ending IP address of the intermediate record.
if($ IP & gt; $endip) {// When the user's ip is greater than the ending IP address of the intermediate record,
$ l = $ I+ 1; //Modify the lower limit of search to intermediate record plus one.
}
Else {// When the user's IP is within the IP range recorded in the middle.
$ findip = $ this-& gt; first IP+$ I * 7;
Break; //means to find the result and exit the loop.
}
}
}
//Get the found IP geographic location information.
fseek($ this-& gt; fp,$ findip);
$ location[' begin IP ']= long 2 IP($ this-& gt; getlong()); //The starting address of the user's IP range.
$ offset = $ this-& gt; get long 3();
fseek($ this-& gt; fp,$ offset);
$ location[' endip ']= long 2 IP($ this-& gt; getlong()); //The end address of the user's IP range.
$ byte = fread($ this-& gt; fp, 1); //Flag byte
switch(order($ byte)){
The flag byte of Case 1: // is 1, which means that country and region information is redirected at the same time.
$ country offset = $ this-& gt; get long 3(); //Redirect address
fseek($ this-& gt; fp,$ country offset);
$ byte = fread($ this-& gt; fp, 1); //Flag byte
switch(order($ byte)){
Case 2://The flag byte is 2, indicating that the country information is redirected again.
fseek($ this-& gt; fp,$ this-& gt; getlong 3());
$ location[' country ']= $ this-& gt; getstring();
fseek($ this-& gt; fp,$ country offset+4);
$ location[' area ']= $ this-& gt; getarea();
Break;
Default: // Otherwise, it means that the country information has not been redirected.
$ location[' country ']= $ this-& gt; getstring($ byte);
$ location[' area ']= $ this-& gt; getarea();
Break;
}
Break;
Case 2://The flag byte is 2, indicating that the country information is redirected.
fseek($ this-& gt; fp,$ this-& gt; getlong 3());
$ location[' country ']= $ this-& gt; getstring();
fseek($ this-& gt; fp,$ offset+8);
$ location[' area ']= $ this-& gt; getarea();
Break;
Default: // Otherwise, it means that the country information has not been redirected.
$ location[' country ']= $ this-& gt; getstring($ byte);
$ location[' area ']= $ this-& gt; getarea();
Break;
}
if($ location[' country ']= = " CZ88 . net "){//CZ88。 NET means there is no valid information.
$location['country'] = "unknown";
}
if ($location['area'] == " CZ88。 NET") {
$ location[' area ']= " ";
}
Return to $ location
}
/**
* constructor, open QQWry. Dat file and initialize the information in the class.
*
* @ param string file name
* @ return IpLocation
*/
The function I location($ filename = "QQWry. Dat") {
$ this-& gt; FP = 0;
If (($ this-& gt;; fp = @fopen($filename,' rb ')! = = False)
$ this-& gt; first IP = $ this-& gt; getlong();
$ this-& gt; lastip = $ this-& gt; getlong();
$ this-& gt; totalip =($ this-& gt; lastip-$ this-& gt; first IP)/7;
//Register the destructor to be executed at the end of program execution.
Register closing function (array (&$ this,' _ ilocation'));
}
}
/**
* destructor, used to automatically close the open file after the page is executed.
*
*/
function _ I location(){
If ($ this->; fp) {
fclose($ this-& gt; FP);
}
$ this-& gt; FP = 0;
}
}
& gt