Home>

The system of thinkphp has a built-in get_client_ip method to obtain the client's IP address. An example is used:

$ip=get_client_ip ();

In addition to thinkphp's 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 IP address, 1 returns IPV4 address numberThe sharing code is as follows

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
  $long=ip2long ($ip);
  $ip=$long array array ($ip, $long):array ("0.0.0.0", 0);
  return $ip [$type];
php
  • Previous PHP simply uses PDO in the database abstraction layer
  • Next Guide to using arrays in Swift programming