Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find my server's IP address in PHP(CLI)

Tags:

php

ip-address

People also ask

How can I know my server IP address in PHP?

In order to obtain the IP address of the server one can use ['SERVER_ADDR'], it returns the IP address of the server under the current script is executing. Another method is using the ['REMOTE_ADDR'] in the $_SERVER array.

How do I find my servers IP address?

First, click on your Start Menu and type cmd in the search box and press enter. A black and white window will open where you will type ipconfig /all and press enter. There is a space between the command ipconfig and the switch of /all. Your ip address will be the IPv4 address.

What is PHP CLI server?

PHP's Command Line Interface (CLI) allows you to execute PHP scripts when logged in to your server through SSH. ServerPilot installs multiple versions of PHP on your server so there are multiple PHP executables available to run.

What is $_ server [' Remote_addr ']?

$_SERVER['REMOTE_ADDR'] Returns the IP address from where the user is viewing the current page. $_SERVER['REMOTE_HOST'] Returns the Host name from where the user is viewing the current page. $_SERVER['REMOTE_PORT']


You can get the hostname by using gethostname


try this it should return the ip address of the server

$host= gethostname();
$ip = gethostbyname($host);

If you are working with PHP < 5.3, this may help (on *NIX based systems atleast):

 mscharley@S04:~$ cat test.php
#!/usr/bin/env php
<?php

function getIPs($withV6 = true) {
    preg_match_all('/inet'.($withV6 ? '6?' : '').' addr: ?([^ ]+)/', `ifconfig`, $ips);
    return $ips[1];
}

$ips = getIPs();
var_dump($ips);

 mscharley@S04:~$ ./test.php
array(5) {
  [0]=>
  string(13) "72.67.113.141"
  [1]=>
  string(27) "fe80::21c:c0ff:fe4a:d09d/64"
  [2]=>
  string(13) "72.67.113.140"
  [3]=>
  string(9) "127.0.0.1"
  [4]=>
  string(7) "::1/128"
}
 mscharley@S04:~$

Or, if you don't anticipate doing it often, then perhaps this would work (just don't abuse it):

$ip = file_get_contents('http://whatismyip.org/');

I know this is a fairly old question, but there doesn't seem to be a definitive answer (in as much as one is possible.) I've had a need to determine this value, both on *NIX boxes and on Win X boxes. Also from a CLI executed script as well as a non-CLI script. The following function is the best I've come up with, which borrows on different concepts people have spoke of over the years. Maybe it can be of some use:

function getServerAddress() {
    if(isset($_SERVER["SERVER_ADDR"]))
    return $_SERVER["SERVER_ADDR"];
    else {
    // Running CLI
    if(stristr(PHP_OS, 'WIN')) {
        //  Rather hacky way to handle windows servers
        exec('ipconfig /all', $catch);
        foreach($catch as $line) {
        if(eregi('IP Address', $line)) {
            // Have seen exec return "multi-line" content, so another hack.
            if(count($lineCount = split(':', $line)) == 1) {
            list($t, $ip) = split(':', $line);
            $ip = trim($ip);
            } else {
            $parts = explode('IP Address', $line);
            $parts = explode('Subnet Mask', $parts[1]);
            $parts = explode(': ', $parts[0]);
            $ip = trim($parts[1]);
            }
            if(ip2long($ip > 0)) {
            echo 'IP is '.$ip."\n";
            return $ip;
            } else
            ; // TODO: Handle this failure condition.
        }
        }
    } else {
        $ifconfig = shell_exec('/sbin/ifconfig eth0');
        preg_match('/addr:([\d\.]+)/', $ifconfig, $match);
        return $match[1];
    }
    }
}

If all else fails, you could always exec ipconfig or ifconfig, depending on your platform, and parse the result.