ThinkPHP uses the UTFWry address library for IP address locating. The extension class can also be included in the official http://www.thinkphp1.cn/extend/223.htmldownload iplocation extension class library, download expansion package (http://www.thinkphp1.cn/down/253.html) also contains the extension class. If the upload class Library is downloaded separately, put the decompressed IpLocation. class. php in the ThinkPHP/Extend/Library/ORG/Net/Directory (if not, manually create it.
1. obtain the IP address
If you only need to obtain the IP address accessed by the user, you can directly use the built-in get_client_ip function, which is a built-in method of ThinkPHP standard mode and can be directly used, it has better compatibility than the built-in PHP system variable $ _ SERVER ['http _ CLIENT_IP ']. usage:
The code is as follows:
$ Ip = get_client_ip ();
Get_client_ip supports multiple IP address detection and legality verification. the returned value is the obtained IP address. if the obtained IP address is invalid, 0.0.0.0 is returned.
If necessary, you can also return IPV4 address numbers, for example:
The code is as follows:
$ Ip = get_client_ip (1 );
The returned results may be similar:
The code is as follows:
2130706433
It can be used for address range and comparison.
II. ip address locating
Obtaining IP addresses alone does not fully meet application requirements. you can only record the requirements for log analysis in the future. The IP address locating function allows you to obtain the region of a user. To use the IP location function, in addition to the IpLocation extension class library, the IP address library file is also required. because ThinkPHP uses UTF8 encoding by default, it is best to use the UTF8 format IP address library file, if it is pure gbk encoding IP address library file, you need to get the results of encoding conversion (will be mentioned below), you can download the UTF8 encoding address library file here: http://www.thinkphp1.cn/extend/270.html
After decompression, put UTFWry. dat in the directory where the IpLocation extension class library is located.
Usage:
The code is as follows:
Import ('org. Net. iplocation'); // import the IpLocation class
$ Ip = new IpLocation (); // instantiate the class
$ Location = $ Ip-> getlocation ('192. 79.93.194 '); // Obtain the location of an Ip address
The returned location variable is an array, including:
The code is as follows:
$ Location ['IP'] // ip address
$ Location ['ininip'] // start address of the user IP address range
$ Location ['enabled'] // end address of the user's IP address range
$ Location ['country'] // country or region
$ Location ['region'] // region
Generally, to locate an IP address, we only need to obtain the country and area Information:
The code is as follows:
$ Info = $ location ['country']. $ location ['region'];
If the IP address library file you are using is not UTFWry. dat (note that the file names in Linux must be in the same case), we need to input the address library file name when instantiating the IpLocation class, for example:
The code is as follows:
$ Ip = new IpLocation ('myipwry. dat '); // input the Ip address library file name
If your IP address Library is GBK encoded, encode and convert the returned results. For example:
The code is as follows:
$ Info = iconv ('gbk', 'utf-8', $ location ['country']. $ location ['region']);
If no parameter is input when the getlocation method is called, the system automatically calls the get_client_ip function above to obtain the current IP address:
The code is as follows:
$ Location = $ Ip-> getlocation ();
You can also pass in domain names to automatically obtain IP addresses.
The code is as follows:
Import ('org. Net. iplocation'); // import the IpLocation class
$ Ip = new IpLocation (); // instantiate the class
$ Area = $ Ip-> getlocation ('www .thinkphp1.cn '); // Obtain the location of the domain name server
Dump ($ area );
Running result output:
If you are using pure IP address library, or often need to change different Address Library, in order to facilitate the IP address location query, you can also separately encapsulate a function to obtain location information, refer to here: http://www.thinkphp1.cn/code/88.html
When http://www.thinkphp1.cn/extend/223.htmldownload ip (http://www.thinkphp1.cn/down/253.html) contains the extension class...