如何在joomla支付网关扩展中获取客户IP地址

时间:2017-09-06 15:00:09

标签: php

以下代码在joomla中不起作用:

code1:不在joomla这个代码中工作

echo $_SERVER['REMOTE_ADDR'];

代码2:不在代码下面的joomla中工作

function getUserIP() {
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    else if(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }

    return $ip;
}


$user_ip = getUserIP();

echo $user_ip;
你能帮帮我吗。

2 个答案:

答案 0 :(得分:0)

How to get visitor's IP Address

使用此代码:

   function checkIPAddress()
   {
      // Get IP Address using $_SERVER['REMOTE_ADDR'];
      $ipaddress = ($_SERVER('REMOTE_ADDR')) ? $_SERVER('REMOTE_ADDR') : '';

       if ( filter_var ($ipaddress, FILTER_VALIDATE_IP) == false)
       {
          $ipaddress = '';
       }
       return $ipaddress;
   }

允许HTTP_X_FORWARDED是一个坏习惯。在进行代理服务器,负载平衡等时使用它

答案 1 :(得分:0)

function getUserIP() {
    $ip = null;
    if (!empty($_SERVER['HTTP_CLIENT_IP']))
    {
        $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
    {
        $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
        $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

$user_ip = getUserIP();
echo $user_ip;