You can download the IP positioning extension class library at http://www.thinkphp.cn/extend/223.html on the official website, or download the expansion package (http://www.thinkphp.cn/down/253.html) This extension class is already included. If the uploaded class library is downloaded separately, put the decompressed IpLocation.class.php under the ThinkPHP/Extend/Library/ORG/Net/ (please create it manually if it does not exist) directory.
1. Obtain IP address
If you only need to obtain the IP address visited by the user, then directly use the system's built-in get_client_ip function. This function is a built-in method in ThinkPHP standard mode and can be used directly. It is better than PHP's built-in system variable $_SERVER['HTTP_CLIENT_IP' ] has better compatibility, usage:
get_client_ip supports various detection and legality verification of IP addresses. The return value is the obtained IP address. If the obtained IP address is illegal, 0.0.0.0 will be returned.
If necessary, you can also return the IPV4 address number, for example:
The returned result may be similar to:
2. IP address positioning
Merely obtaining the IP address cannot fully meet the needs of the application. It can only be recorded for future log analysis needs. The IP address positioning function allows you to obtain the user's area. To use the IP location function, in addition to the IpLocation extension class library, you also need an IP address library file. Since ThinkPHP uses UTF8 encoding by default, it is best to use an IP address library file in UTF8 format. If it is a pure gbk encoded IP address library file, you need to perform encoding conversion on the obtained results (mentioned below). You can download the UTF8 encoded address library file here: http://www.thinkphp.cn/extend/270.html
Decompressed address The library file UTFWry.dat can be placed under the directory where the IpLocation extension class library is located.
Usage:
The location variable returned is an array, including:
Usually, if we want to get IP positioning, we only need to get the country and area information:
If the IP address library file you use is not UTFWry.dat (note that the case of the file name under Linux also needs to be consistent), we need to pass in the address library file name when instantiating the IpLocation class, for example:
If your IP address library is encoded in GBK, you need to perform encoding conversion on the returned result. For example:
If no parameters are passed in when calling the getlocation method, the system will automatically call the get_client_ip function above to obtain the current IP address:
You can also pass in a domain name to automatically obtain an IP address
If you are using a pure IP address library, or often need to change different address libraries, in order to facilitate IP location query, you can also encapsulate a separate function to obtain location information, refer to here: http://www.thinkphp .cn/code/88.html