PHP client IP retrieval method

Source: Internet
Author: User
In fact, there are many ways to use PHP to obtain the client IP address on the internet. I have collected some methods before, but here I mainly recommend the thinkphp framework, after all, I have been using the thinkphp framework and its method all the time.
/*** Get client IP address * @ param integer $ type return type 0 return IP address 1 return IPV4 address number * @ return mixed */function get_client_ip ($ type = 0) {$ type = $ type? 1: 0; static $ ip = NULL; if ($ ip! = NULL) return $ ip [$ type]; if (isset ($ _ SERVER ['http _ X_FORWARDED_FOR ']) {$ arr = explode (',', $ _ SERVER ['http _ X_FORWARDED_FOR ']); $ pos = array_search ('Unknown', $ arr); if (false! ==$ Pos) unset ($ arr [$ pos]); $ ip = trim ($ arr [0]);} elseif (isset ($ _ SERVER ['http _ CLIENT_IP ']) {$ ip = $ _ SERVER ['http _ CLIENT_IP'];} elseif (isset ($ _ SERVER ['remote _ ADDR ']) {$ ip = $ _ SERVER ['remote _ ADDR'];} // valid ip address verification $ long = sprintf ("% u", ip2long ($ ip); $ ip = $ long? Array ($ ip, $ long): array ('0. 0.0.0 ', 0); return $ ip [$ type];}

The above is the function that comes with the thinkphp framework to obtain the client IP address. the call method is as follows:

get_client_ip();

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.