PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > ThinkPhp教程 > 正文

    thinkphp获取客户端ip get_client_ip函数

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:thinkphp框架中系统内置了get_client_ip方法用于获取客户端的IP地址,使用示例:$ip = get_client_ip();除了thinkphp内置get_client_ip函...
    thinkphp框架中系统内置了get_client_ip方法用于获取客户端的IP地址,使用示例:

    $ip = get_client_ip();

    除了thinkphp内置get_client_ip函数外,也可使用下面函数获取客户端IP地址。

    $type表示返回类型 0 返回IP地址, 1 返回IPV4地址数字

    1. function get_client_ip($type = 0) { 
    2.     $type       =  $type ? 1 : 0; 
    3.     static $ip  =   NULL; 
    4.     if ($ip !== NULL) return $ip[$type]; 
    5.     if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { 
    6.         $arr    =   explode(','$_SERVER['HTTP_X_FORWARDED_FOR']); 
    7.         $pos    =   array_search('unknown',$arr); 
    8.         if(false !== $pos) unset($arr[$pos]); 
    9.         $ip     =   trim($arr[0]); 
    10.     }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { 
    11.         $ip     =   $_SERVER['HTTP_CLIENT_IP']; 
    12.     }elseif (isset($_SERVER['REMOTE_ADDR'])) { 
    13.         $ip     =   $_SERVER['REMOTE_ADDR']; 
    14.     } 
    15.     // IP地址合法验证 
    16.     $long = ip2long($ip); 
    17.     $ip   = $long ? array($ip$long) : array('0.0.0.0', 0); 
    18.     return $ip[$type]; 

     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-83-2107-1.html
    相关热词搜索: