Why is $_SERVER["REMOTE_ADDR"] returning multiple IP Addresses?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deko

    Why is $_SERVER["REMOTE_ADDR"] returning multiple IP Addresses?

    Why is $_SERVER["REMOTE_ADD R"] returning multiple IP Addresses?

    Actually, I'm not sure if it's $_SERVER["REMOTE_ADD R"] -- or which if/else
    statement -- that's the problem, but what I'm getting as a value for $visip
    looks like this:

    172.16.42.181, 62.138.35.94

    Why am I getting more than one IP Address? Which IP is the originating IP
    Address? Is there a way to get only the originating IP?

    Here's the code:


    if (isset($_SERVER ))
    {
    if (isset($_SERVER["HTTP_X_FORWARD ED_FOR"]))
    {
    $visip = $_SERVER["HTTP_X_FORWARD ED_FOR"];
    }
    elseif (isset($_SERVER["HTTP_CLIENT_IP "]))
    {
    $visip = $_SERVER["HTTP_CLIENT_IP "];
    }
    else
    {
    $visip = $_SERVER["REMOTE_ADD R"];
    }
    }
    else
    {
    if (getenv('HTTP_X _FORWARDED_FOR' ))
    {
    $visip = getenv('HTTP_X_ FORWARDED_FOR') ;
    }
    elseif (getenv('HTTP_C LIENT_IP'))
    {
    $visip = getenv('HTTP_CL IENT_IP');
    }
    else
    {
    $visip = getenv('REMOTE_ ADDR');
    }
    }

    You can view the page that displays the results here:



    (scroll to entry for Oct 29 2004 10:17 am and Oct 30 2004 05:26 am)

    Thanks in advance.


  • Alvaro G. Vicario

    #2
    Re: Why is $_SERVER["REMOTE_AD DR"] returning multiple IP Addresses?

    *** deko escribió/wrote (Sat, 30 Oct 2004 17:34:24 GMT):[color=blue]
    > Why is $_SERVER["REMOTE_ADD R"] returning multiple IP Addresses?
    >
    > Actually, I'm not sure if it's $_SERVER["REMOTE_ADD R"] -- or which if/else
    > statement --[/color]

    $_SERVER["HTTP_X_FORWARD ED_FOR"] is a comma-separated list of IP addresses,
    though it's normally a one element list. This variable is the result of
    intermediate proxies.

    This function gets the first element:

    function real_ip(){
    if($tmp=$_SERVE R['HTTP_X_FORWARD ED_FOR']){
    $tmpip=explode( ",", $tmp);
    return trim($tmpip[0]);
    }else return $_SERVER['REMOTE_ADDR'];
    }


    --
    -+ Álvaro G. Vicario - Burgos, Spain
    +- http://www.demogracia.com (la web de humor barnizada para la intemperie)
    ++ Las dudas informáticas recibidas por correo irán directas a la papelera
    -+ I'm not a free help desk, please don't e-mail me your questions
    --

    Comment

    • deko

      #3
      Re: Why is $_SERVER["REMOTE_AD DR"] returning multiple IP Addresses?

      > $_SERVER["HTTP_X_FORWARD ED_FOR"] is a comma-separated list of IP
      addresses,[color=blue]
      > though it's normally a one element list. This variable is the result of
      > intermediate proxies.[/color]

      Ah, I see. Thanks for the help!


      Comment

      • deko

        #4
        Re: Why is $_SERVER["REMOTE_AD DR"] returning multiple IP Addresses?

        Follow up question:

        How do I call the RealIp function? What I have below is not working.

        function realIp()
        {
        $ip_array=explo de(",", $visip);
        return trim($ip_array[0]);
        }
        if (isset($_SERVER ))
        {
        if (isset($_SERVER["HTTP_X_FORWARD ED_FOR"]))
        {
        $visip = $_SERVER["HTTP_X_FORWARD ED_FOR"];
        $visip = realIp();
        }
        elseif (isset($_SERVER["HTTP_CLIENT_IP "]))
        {
        $visip = $_SERVER["HTTP_CLIENT_IP "];
        }
        else
        {
        $visip = $_SERVER["REMOTE_ADD R"];
        }
        }
        else
        {
        if (getenv('HTTP_X _FORWARDED_FOR' ))
        {
        $visip = getenv('HTTP_X_ FORWARDED_FOR') ;
        $visip = realIp();
        }
        elseif (getenv('HTTP_C LIENT_IP'))
        {
        $visip = getenv('HTTP_CL IENT_IP');
        }
        else
        {
        $visip = getenv('REMOTE_ ADDR');
        }
        }


        Comment

        • Alvaro G. Vicario

          #5
          Re: Why is $_SERVER["REMOTE_AD DR"] returning multiple IP Addresses?

          *** deko escribió/wrote (Sat, 30 Oct 2004 19:33:01 GMT):[color=blue]
          > function realIp()
          > {
          > $ip_array=explo de(",", $visip);
          > return trim($ip_array[0]);
          > }[/color]

          I don't know what your code tries to do but if you want to use variables
          that are external to the function you must either:

          1) Pass them as parameters:

          function realIp($visip)
          {
          ...
          }


          2) Declare them as global:

          funtion realIp()
          {
          global $visip;
          ...
          }


          --
          -+ Álvaro G. Vicario - Burgos, Spain
          +- http://www.demogracia.com (la web de humor barnizada para la intemperie)
          ++ Las dudas informáticas recibidas por correo irán directas a la papelera
          -+ I'm not a free help desk, please don't e-mail me your questions
          --

          Comment

          • Tim Roberts

            #6
            Re: Why is $_SERVER["REMOTE_AD DR"] returning multiple IP Addresses?

            "deko" <www-dot-clearpointsyste ms-dot-com@nospam.com> wrote:
            [color=blue]
            >Follow up question:
            >
            >How do I call the RealIp function? What I have below is not working.
            >
            >function realIp()
            > {
            > $ip_array=explo de(",", $visip);
            > return trim($ip_array[0]);
            > }
            >if (isset($_SERVER ))
            > {
            > if (isset($_SERVER["HTTP_X_FORWARD ED_FOR"]))
            > {
            > $visip = $_SERVER["HTTP_X_FORWARD ED_FOR"];
            > $visip = realIp();
            > }[/color]


            Don't rely on globals.

            function realIp($unrealI p)
            {
            $ip_array = explode(",", $unrealIp);
            return trim($ip_array[0]);
            }
            ....
            {
            $visip = realIp($_SERVER['HTTP_X_FORWARD ED_FOR"]);
            }

            What makes you think the first IP in the list is any more "real" than any
            of the others? Do you hope to use this for some kind of validation? If
            so, the numbers are quite unreliable.
            --
            - Tim Roberts, timr@probo.com
            Providenza & Boekelheide, Inc.

            Comment

            • Gary L. Burnore

              #7
              Re: Why is $_SERVER[&quot;REMOTE_AD DR&quot;] returning multiple IP Addresses?

              On Sun, 31 Oct 2004 14:39:17 -0800, Tim Roberts <timr@probo.com >
              wrote:
              [color=blue]
              >"deko" <www-dot-clearpointsyste ms-dot-com@nospam.com> wrote:
              >[color=green]
              >>Follow up question:
              >>
              >>How do I call the RealIp function? What I have below is not working.
              >>
              >>function realIp()
              >> {
              >> $ip_array=explo de(",", $visip);
              >> return trim($ip_array[0]);
              >> }
              >>if (isset($_SERVER ))
              >> {
              >> if (isset($_SERVER["HTTP_X_FORWARD ED_FOR"]))
              >> {
              >> $visip = $_SERVER["HTTP_X_FORWARD ED_FOR"];
              >> $visip = realIp();
              >> }[/color]
              >
              >
              >Don't rely on globals.
              >
              >function realIp($unrealI p)
              >{
              > $ip_array = explode(",", $unrealIp);
              > return trim($ip_array[0]);
              >}
              >...
              > {
              > $visip = realIp($_SERVER['HTTP_X_FORWARD ED_FOR"]);
              > }
              >
              >What makes you think the first IP in the list is any more "real" than any
              >of the others? Do you hope to use this for some kind of validation? If
              >so, the numbers are quite unreliable.[/color]


              Exactly. If there's more than one IP, you'd have to validate for each
              one or all of them at once.



              --
              gburnore@databa six dot com
              ---------------------------------------------------------------------------
              How you look depends on where you go.
              ---------------------------------------------------------------------------
              Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
              | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
              DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
              | ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
              Black Helicopter Repair Svcs Division | Official Proof of Purchase
              =============== =============== =============== =============== ===============
              Want one? GET one! http://signup.databasix.com
              =============== =============== =============== =============== ===============

              Comment

              • R. Rajesh Jeba Anbiah

                #8
                Re: Why is $_SERVER[&quot;REMOTE_AD DR&quot;] returning multiple IP Addresses?

                "deko" <www-dot-clearpointsyste ms-dot-com@nospam.com> wrote in message news:<AgQgd.368 18$QJ3.33665@ne wssvr21.news.pr odigy.com>...
                <snip>[color=blue]
                > Is there a way to get only the originating IP?[/color]

                <snip>

                Try i2c_realip() available at
                <http://in2.php.net/source.php?url=/include/ip-to-country.inc>

                --
                | Just another PHP saint |
                Email: rrjanbiah-at-Y!com

                Comment

                Working...