Thinkphp the method of obtaining client IP and IP address query _php instance

Source: Internet
Author: User
Tags php template smarty template

The example in this article describes how thinkphp obtains client IP and IP address queries. Share to everyone for your reference, specific as follows:

The public function of the system that obtains the client IP address in TP is the function get_client_ip (). The return value is the IP address.

The class file of the country and region where the IP address is queried is IpLocation.class.php, located in the Thinkphp\lib\org\net directory. The class name is Iplocation, and the method is

Public Function getlocation ($ip = ');

Query client IP address when omitted. Returns an array. The return value is null if it is not found; the value of the array is:

$location [' IP '];
$location [' Country '];
$location [' area '];

The specific application process for querying client IP and its corresponding area is:

Import (' ORG.Net.IpLocation ');
$ip =new iplocation ()//Create a new IP query object
$location = $ip->getlocation (), or the query client IP when omitted. $location can be output directly using the key name as above.

If you want to query for a specific IP or domain name, the procedure is:

Import (' ORG.Net.IpLocation ');
$ip =new iplocation ()//Create a new IP query object
$ipname = ' 202.38.68.68′;//or can use domain names, such as $ipname= ' www.64hi.com ';
$location = $ip->getlocation ($ipname);//$location can be output directly using the key name as above.

If you only query the client IP address, you can use the system function directly, such as:

$ip = $ip->get_client_ip ();
Many sites are $ip = Get_client_ip (); In fact, all are wrong, we should pay attention to.

More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course", "thinkphp Template Operation Skills Summary", "thinkphp Common Methods Summary", "Smarty Template Introductory Course" and "PHP template technology Summary."

I hope this article will help you with the PHP program design based on thinkphp framework.

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.