Is there a way to detect the name of the server running a PHP script from the command line?
There are numerous ways to do this for PHP accessed via HTTP. But there does not appear to be a way to do this for CLI.
For example:
$_SERVER['SERVER_NAME']
is not available from the command line.
Possibly because if you run a script from the command line, no server is involved?
See http://php.net/manual/en/function.gethostname.php
gethostname() is a convenience function which does the same as
php_uname('n')
and was added as of PHP 5.3One of these may have the server name:
Gives me the name of the server.
In order to get the hostname from the commandline you would need to also run using php -r. On linux I used:
On windows I used:
Additionally, when accessed via the console PHP does not provide many of the classic $_SERVER values but on the server I accessed only has the following keys:
see http://php.net/manual/en/function.php-uname.php
Call the CLI and use the gethostname command:
Prints: