thinkphp How to get the client ip,thinkphp get ip_php tutorial

Source: Internet
Author: User
Tags get ip

thinkphp How to get the client ip,thinkphp get IP


In the thinkphp framework, the system built-in Get_client_ip method is used to obtain the IP address of the client, using the example:
$ip = Get_client_ip ();
In addition to the thinkphp built-in get_client_ip function, you can also use the following function to obtain the client IP address.
$type means return type 0 returns an IP address, 1 returns a IPV4 address number
Share the code below

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 '];  }  IP Address legal authentication  $long = Ip2long ($IP);  $ip  = $long? Array ($ip, $long): Array (' 0.0.0.0 ', 0);  return $ip [$type];

I hope this article will be helpful to you in-depth study of PHP programming.

http://www.bkjia.com/PHPjc/1067838.html www.bkjia.com true http://www.bkjia.com/PHPjc/1067838.html techarticle thinkphp How to get the client ip,thinkphp get the IP thinkphp framework in the system built-in Get_client_ip method used to get the IP address of the client, using example: $ip = 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.