Thinkphp obtains the Client IP address and IP address query

Source: Internet
Author: User

The system public function for obtaining the Client IP address in TP is: function get_client_ip (). The returned value is the IP address.

The class file for querying the country and region of the IP address is iplocation. Class. php, which is located in the thinkphp \ Lib \ org \ net directory. The class name is iplocation, and the method is public function getlocation ($ IP = "); // if it is omitted, query the IP address of the client. Returns an array. If no value is found, the returned value is null. If yes, the array value is $ location ['IP']; $ location ['country']; $ location ['region']; the specific application process for querying the Client IP address and its corresponding region is: Import ('org. net. iplocation '); $ IP = new iplocation (); // create an IP query object $ location = $ IP-> getlocation (); // if it is omitted, the Client IP address is queried. $ location can be output directly, with the key name as above. To query the region of a specific IP address or domain name, the process is: Import ('org. net. iplocation '); $ IP = new iplocation (); // create an IP query object $ ipname = '2017. 38.68.68 '; // or you can use a domain name, such as $ ipname = 'www .64hi.com'; $ location = $ IP-> getlocation ($ ipname ); // $ location can be output directly, with the key name as above. If you only need to query the Client IP address, you can directly use the system function, such as: $ IP = $ IP-> Get_client_ip (); // many websites use $ IP = Get_client_ip (); in fact, they are all incorrect.

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.