THinkPHP queries the Client IP address and IP address.

Source: Internet
Author: User

THinkPHP queries the Client IP address and IP address.

This example describes how THinkPHP can query the Client IP address and IP address. We will share this with you for your reference. The details are as follows:

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 this parameter is omitted, the IP address of the client is queried. Returns an array. If no value is found, the returned value is null. If yes, the value of the array is:

$location['ip'];$location['country'];$location['area'];

The specific application process for querying the Client IP address and the 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 as follows:

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:

$ 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.