39
votes

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.

9
I think I've finally got a semi-decent answer: $server = system('echo $HOSTNAME'); Anyone have something better? - Justin Noel
@Uwe Mesecke : I can't vote up or down. Your answer is the best. Although, it is not 100% portable, because some hosts respond with extra garbage in that. Mac OS X Leopard and OpenSolaris all respond with just the hostname. RHEL3 (shudder) responds with all kinds of junk. - Justin Noel
Do you not even have an option to check a tick next to the answer you've chosen? - random
If you need portability, you might want to know that on Windows (XP, running WAMP w/ PHP 5.2.6), the echo $HOSTNAME gives me exactly that, i.e. $HOSTNAME. However, both Uwe Mesecke's and jonstjohn's suggestions both worked on both Windows and Linux (CentOS 5 w/ PHP 5.1.6). - PTBNL
@Random Echo : No, I can't see anyway to indicate the best answer. Voting up or down tells me I don't have a high enough reputation. - Justin Noel

9 Answers

20
votes
<?php
echo gethostname(); // may output e.g,: sandie

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.3

12
votes

The SERVER_NAME is not available when you run PHP from the CLI for that very same reason.

When you run PHP from the CLI, you start your own PHP intepreter that runs whatever code you passed to it, without any kind of server. So from the CLI, PHP knows nothing about your web server that you do not explicitly tell it.

3
votes

Try:

$servername = trim(`hostname`);
3
votes

In order to get the hostname from the commandline you would need to also run using php -r. On linux I used:

~#php -r 'echo php_uname("n");'
hostname

On windows I used:

D:\xampp\php>php -r "echo php_uname('n');"
MB-PC

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:

root@hermes:~# php -r 'foreach($_SERVER as $key =>$value){echo $key.",";}'
TERM,SHELL,SSH_CLIENT,SSH_TTY,USER,LS_COLORS,MAIL,PATH,PWD,LANG,SHLVL,HOME,LOGNAME,SSH_CONNECTION,LESSOPEN,LESSCLOSE,_,PHP_SELF,SCRIPT_NAME,SCRIPT_FIL
ENAME,PATH_TRANSLATED,DOCUMENT_ROOT,REQUEST_TIME,argv,argc
3
votes

Call the CLI and use the gethostname command:

php -r "echo gethostname();"

Prints:

your_hostname
3
votes

You can add your php script to command line that will set $_SERVER['SERVER_NAME'] for you. Here is how I'm using it:

C:\SDK\php-5.5.25-Win32-VC11-x86\php.exe -S localhost:8080 -t D:\Projects\Sites\mysite router.php

router.php

<?php
$_SERVER['SERVER_NAME'] = 'localhost:8080';
return false;    // serve the requested resource as-is.
?> 
2
votes

Possibly because if you run a script from the command line, no server is involved?

-6
votes

One of these may have the server name:

print_r($_SERVER)
var_dump($_SERVER) 
echo $_SERVER['HOSTNAME']

Gives me the name of the server.