Is there anyway in PHP to reliably get the server's IP address? I can't use $SERVER due to the fact that the script is also called directly, not via a web server - so $SERVER doesn't exist.

Is there a function or something to get it, because I can't seem to find one... ?

TIA

Regards

Steve.

    what do you mean the script is called directly, not via a webserver

    you doing it command line

      9 days later

      C'mon - please, someone must know how to get the IP address of the computer that the script is being run on? Its on a Linux box btw.

      Please, this is an urgent question as I need to fix a problem on a client's site.

        Originally posted by laserlight
        Use system() or exec() with say, ping?

        Yeah, but I need to know the IP of the server, Ping doesn't output that does it?

        Reason being that the script can be run on 2 servers, so that it can setup the paths correctly it needs to know which machine its on, the only way of doing that, that I can see is to detect the IP address of the server.

        Thanks LaserLight, for your suggestion though, please keep 'em coming! 🙂

        Regards

        Steve Childs.

          But on a Windows machine - you could use IPCONFIG.... (2k + only though I think...)

            Yeah, problem is its on a Linux box! 😉 - The other server is a NT box, so yes, that could potientally work on that machine. Surely there must be an easy and portable way to detect the server's IP address in PHP? Or have I just found an oversight in PHP! 😉

              you can use ifconfig on a linux box to get all the network information like ipconfig on an nt box

                I stand corrected...

                (and admit my severe lack of all things Linux based... :mad: )

                  I came up with a quick and dirty way to do it from a command line script:

                  #!/usr/local/bin/php -q
                  <?php
                  
                  /* path to ifconfig */
                  $ifconfig = "/sbin/ifconfig";
                  
                  /* path to grep */
                  $grep = "/bin/grep";
                  
                  /*     
                  * get output from ifconfig * example output:
                  * inet addr:192.168.254.10 Bcast:192.168.254.255 Mask:255.255.255.0 */ exec("$ifconfig eth0 | $grep 'inet addr'", $net_info); /* Find the location of the "B" in order to strip away the excess later. */
                  $B_loc = strpos($net_info[0], "B"); /* Find the location of the colon to strip out the characters before the IP address. */ $col_loc = strpos($net_info[0], ":"); /* Return everything before the "B". */ $first = substr($net_info['0'], 0, $B_loc); /* Return the string after "inet addr". The +1 in the second parameter is used to strip off the colon. */ $ip = substr($first, $col_loc+1, 15); /* display our IP */ echo $ip . "\n"; ?>

                  Hope that helps.

                  -a9

                    Cheers, I'll give that a try.

                    Thanks for the time and effort put into that script! 🙂

                      Write a Reply...