How to get clients ip in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • E Guru
    Confirmed User
    • Jul 2005
    • 658

    #1

    How to get clients ip in php

    I am having a problem with some server load sharing and getting the clients correct ip.
    When I use xforwardedfor it gets the first machines ip (servers ip) and I use remote_addr it gets the 2nd machines ip.
    I tried using ['HTTP_CLIENT_IP'] but it shows ip blank.

    Does anyone know what I can use grab the ip from the users browser?
    Guru
  • broke
    Confirmed User
    • Aug 2003
    • 4501

    #2
    ['REMOTE_ADDR']
    Last edited by broke; 08-29-2005, 09:16 AM.
    Perfect Gonzo

    Comment

    • E Guru
      Confirmed User
      • Jul 2005
      • 658

      #3
      Originally posted by broke
      ['REMOTE_ADDR']
      That doesn't work as I said above
      Guru

      Comment

      • broke
        Confirmed User
        • Aug 2003
        • 4501

        #4
        Don't mind me... I'm retarded.
        Perfect Gonzo

        Comment

        • Calvinguy
          Confirmed User
          • Oct 2002
          • 1752

          #5
          $_server["remote_addr"]

          Comment

          • Manowar
            jellyfish  
            • Dec 2003
            • 71528

            #6
            Originally posted by Calvinguy
            $_server["remote_addr"]

            Comment

            • E Guru
              Confirmed User
              • Jul 2005
              • 658

              #7
              Originally posted by Calvinguy
              $_server["remote_addr"]
              No, that does not work. The whole variable does not work.

              Here is what they told me.
              Its passed as a variable
              "x-client-ip"
              in unix if you want to set the log to read it, you would use this:
              LogFormat "\"%{X-Client-IP}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

              Well, how would I get that in php?
              Guru

              Comment

              • jwerd
                Confirmed User
                • Jun 2003
                • 1953

                #8
                If
                $_SERVER['REMOTE_ADDR'] doesn't work, superglobals may be broken

                Try:

                $ip = getenv('REMOTE_ADDR');
                echo $ip
                Yii Framework Guru - Seasoned PHP vet - Partner @ XXXCoupon.com

                Comment

                • Quickdraw
                  Confirmed User
                  • Mar 2004
                  • 1717

                  #9
                  $_server[http_pc_remote_addr]

                  Comment

                  • E Guru
                    Confirmed User
                    • Jul 2005
                    • 658

                    #10
                    Originally posted by lamerhooD
                    If
                    $_SERVER['REMOTE_ADDR'] doesn't work, superglobals may be broken

                    Try:

                    $ip = getenv('REMOTE_ADDR');
                    echo $ip
                    No man, it works. It just doesn't get the correct ip.
                    Guru

                    Comment

                    • E Guru
                      Confirmed User
                      • Jul 2005
                      • 658

                      #11
                      Originally posted by Quickdraw
                      $_server[http_pc_remote_addr]
                      Comes up blank
                      Guru

                      Comment

                      • 4Pics
                        Confirmed User
                        • Dec 2001
                        • 7952

                        #12
                        Are you using a proxy?

                        Comment

                        • broke
                          Confirmed User
                          • Aug 2003
                          • 4501

                          #13
                          Have you tried x-forwarded-client?
                          Perfect Gonzo

                          Comment

                          • E Guru
                            Confirmed User
                            • Jul 2005
                            • 658

                            #14
                            Originally posted by 4Pics
                            Are you using a proxy?
                            Not myself, but server seams thinks I am. That is why I am trying to grab the IP from the users browser.
                            Guru

                            Comment

                            • E Guru
                              Confirmed User
                              • Jul 2005
                              • 658

                              #15
                              Originally posted by broke
                              Have you tried x-forwarded-client?

                              Returns a 0
                              Guru

                              Comment

                              • broke
                                Confirmed User
                                • Aug 2003
                                • 4501

                                #16
                                How about $_GET['x-client-ip']
                                Perfect Gonzo

                                Comment

                                • E Guru
                                  Confirmed User
                                  • Jul 2005
                                  • 658

                                  #17
                                  Originally posted by broke
                                  How about $_GET['x-client-ip']
                                  No go, comes up blank. You would think that would do it though.
                                  Guru

                                  Comment

                                  • E Guru
                                    Confirmed User
                                    • Jul 2005
                                    • 658

                                    #18
                                    This is the code I used

                                    <?php

                                    $RIGHT_IP = $_GET['x-client-ip'];

                                    ?>

                                    <? echo "$RIGHT_IP"; ?>
                                    Guru

                                    Comment

                                    • E Guru
                                      Confirmed User
                                      • Jul 2005
                                      • 658

                                      #19
                                      Here is what they just said also:
                                      If it were in unix it would be
                                      LogFormat "\"%{X-Client-IP}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined


                                      Wtf does that mean?
                                      Guru

                                      Comment

                                      • Quickdraw
                                        Confirmed User
                                        • Mar 2004
                                        • 1717

                                        #20
                                        Have you tried echo phpinfo(); to see what might work?

                                        I grabbed this function from php.net and added the echo.. maybe?

                                        PHP Code:
                                        function getip() {
                                           if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
                                           $ip = getenv("HTTP_CLIENT_IP");
                                        
                                           else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
                                           $ip = getenv("HTTP_X_FORWARDED_FOR");
                                        
                                           else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
                                           $ip = getenv("REMOTE_ADDR");
                                        
                                           else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
                                           $ip = $_SERVER['REMOTE_ADDR'];
                                        
                                           else
                                           $ip = "unknown";
                                        
                                           return($ip);
                                        } 
                                        echo getip(); 
                                        

                                        Comment

                                        • E Guru
                                          Confirmed User
                                          • Jul 2005
                                          • 658

                                          #21
                                          Quikdraw - I tried the phpinfo and found the variable that held the ip

                                          You rock

                                          Got it working
                                          Guru

                                          Comment

                                          • jwerd
                                            Confirmed User
                                            • Jun 2003
                                            • 1953

                                            #22
                                            Originally posted by E Guru

                                            <? echo "$RIGHT_IP"; ?>
                                            An easier way to do this is:

                                            <?=$RIGHT_IP?>

                                            Yii Framework Guru - Seasoned PHP vet - Partner @ XXXCoupon.com

                                            Comment

                                            • broke
                                              Confirmed User
                                              • Aug 2003
                                              • 4501

                                              #23
                                              So what was the variable?
                                              Perfect Gonzo

                                              Comment

                                              • E Guru
                                                Confirmed User
                                                • Jul 2005
                                                • 658

                                                #24
                                                Originally posted by broke
                                                So what was the variable?
                                                HTTP_X_CLIENT_IP
                                                Guru

                                                Comment

                                                Working...