如何在Codeigniter上获取用户的IP地址

时间:2019-09-25 05:23:06

标签: php codeigniter codeigniter-3

我遇到了一些问题,我需要知道如何在我的网站上获取用户的IP地址,并且我正在使用此代码:

$ip = $this->input->ip_address() ;
echo $ip;

这将返回我的主机IP,谢谢您的帮助

1 个答案:

答案 0 :(得分:0)

Use this function instead of it.

  function get_ip_address_of_user() {
   $ipaddress = array();
   if (isset($_SERVER['HTTP_CLIENT_IP']))
    $ipaddress['ip'] = $_SERVER['HTTP_CLIENT_IP'];
   else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
    $ipaddress['ip'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
   else if (isset($_SERVER['HTTP_X_FORWARDED']))
    $ipaddress['ip'] = $_SERVER['HTTP_X_FORWARDED'];
    else if (isset($_SERVER['HTTP_FORWARDED_FOR']))
    $ipaddress['ip'] = $_SERVER['HTTP_FORWARDED_FOR'];
   else if (isset($_SERVER['HTTP_FORWARDED']))
    $ipaddress['ip'] = $_SERVER['HTTP_FORWARDED'];
    else if (isset($_SERVER['REMOTE_ADDR']))
    $ipaddress['ip'] = $_SERVER['REMOTE_ADDR'];
   return $ipaddress;
  }