ThinkPHP3.1 quick start (21) IP address locating

Source: Internet
Author: User
Tags domain name server
In WEB applications, it is also very common to locate and record access logs based on IP addresses. in ThinkPHP, you can easily obtain and locate IP addresses. In WEB applications, it is also very common to locate and record access logs based on IP addresses. in ThinkPHP, you can easily obtain and locate IP addresses.

Get the extension class library can be in the official website http://www.thinkphp.cn/extend/223.htmldownload iplocation extended class library, download package (http://www.thinkphp.cn/down/253.html) has also included 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.

To obtain an 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 used directly, it has better compatibility than the built-in PHP system variable $ _ SERVER ['http _ CLIENT_IP ']. usage:
  1. $ Ip = get_client_ip ();
Copy the code get_client_ip to support 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:
  1. $ Ip = get_client_ip (1 );
The result returned by the copied code may be similar:
  1. 2130706433
The copy code can be used for address range and comparison.

IP address location only obtains IP addresses and does not fully meet application requirements. it can only be recorded for future log analysis needs. the IP address location function allows you to obtain the user's region. 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.thinkphp.cn/extend/270.html
After decompression, put UTFWry. dat in the directory where the IpLocation extension class library is located.
Usage:
  1. Import ('org. Net. iplocation'); // import the IpLocation class
  2. $ Ip = new IpLocation (); // instantiate the class
  3. $ Location = $ Ip-> getlocation ('192. 79.93.194 '); // Obtain the location of an Ip address
The location variable returned by the copy code is an array, including:
  1. $ Location ['IP'] // ip address
  2. $ Location ['ininip'] // start address of the user IP address range
  3. $ Location ['enabled'] // end address of the user's IP address range
  4. $ Location ['country'] // country or region
  5. $ Location ['region'] // region
Copy the code. to locate the IP address, you only need to obtain the country and area Information:
  1. $ Info = $ location ['country']. $ location ['region'];
Copy the code 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:
  1. $ Ip = new IpLocation ('myipwry. dat '); // input the Ip address library file name
Copy the code. if your IP address Library is GBK encoded, encode and convert the returned results. For example:
  1. $ Info = iconv ('gbk', 'utf-8', $ location ['country']. $ location ['region']);
Copy the code. If no parameters are input when the getlocation method is called, the system automatically calls the get_client_ip function above to obtain the current IP address:
  1. $ Location = $ Ip-> getlocation ();
You can also copy the code to pass in a domain name to automatically obtain an IP address.
  1. Import ('org. Net. iplocation'); // import the IpLocation class
  2. $ Ip = new IpLocation (); // instantiate the class
  3. $ Area = $ Ip-> getlocation ('www .thinkphp.cn '); // Obtain the location of the domain name server
  4. Dump ($ area );
Copy the code 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.thinkphp.cn/code/88.html

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.