41

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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Justin Noel
  • 5,945
  • 10
  • 44
  • 59
  • 1
    I think I've finally got a semi-decent answer: $server = system('echo $HOSTNAME'); Anyone have something better? – Justin Noel Mar 09 '09 at 14:36
  • @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 Mar 09 '09 at 14:49
  • Do you not even have an option to check a tick next to the answer you've chosen? – random Mar 09 '09 at 14:52
  • 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 Mar 09 '09 at 14:57
  • @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 Mar 09 '09 at 15:16
  • @PTBNL : On *nix system, the "echo $HOSTNAME" must be done like this "$hostname = system('echo $HOSTANME')". Unfortunately, also echos the hostname to output. This may not be desired because it may confuse users. – Justin Noel Mar 09 '09 at 15:17
  • @AppBeacon: It looks like I wasn't clear, sorry. I was just trying to say that the echo "$HOSTNAME" wouldn't work on Windows. On my Linux box, the code system('echo $HOSTNAME') printed out the name of my system. However, on Windows it printed the string "$HOSTNAME" (quote marks mine). – PTBNL Mar 09 '09 at 17:06

9 Answers9

92
echo php_uname("n");

see http://php.net/manual/en/function.php-uname.php

Uwe Mesecke
  • 1,911
  • 13
  • 11
  • 2
    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 Mar 09 '09 at 14:50
  • Will it works if you have 2 domain names on the same server (IP)? – Genjo Nov 08 '16 at 11:00
  • The machine's hostname and reverse address resolution are different things... uname returns the hostname and AFAIK there should be only one per machine. If you need reverse address lookup you should use a dns client. – Uwe Mesecke Nov 23 '16 at 16:29
  • Hmm just get the pc name, but i need to get localhost. – Luis Alfredo Serrano Díaz May 21 '20 at 18:19
21
<?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

ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
Alexander V. Ilyin
  • 2,440
  • 1
  • 16
  • 11
12

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.

mikl
  • 23,749
  • 20
  • 68
  • 89
3

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
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Menelaos
  • 23,508
  • 18
  • 90
  • 155
3

Call the CLI and use the gethostname command:

php -r "echo gethostname();"

Prints:

your_hostname
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
trond
  • 31
  • 1
3

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

Try:

$servername = trim(`hostname`);
Thomas Hunter II
  • 5,081
  • 7
  • 35
  • 54
jonstjohn
  • 59,650
  • 8
  • 43
  • 55
  • 1
    this seems plain wrong. trim does only remove whitespaces. how the hell should that work with a static string as input? – Benjamin Eckstein Nov 18 '16 at 11:33
  • in this case it executes the linux command `hostname`, then the returned string is trimmed... this trick will work in a linux/unix environment only. Try this example: echo \`hostname`; – Nicolai Nita May 17 '17 at 10:59
2

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

  • 1
    No server in the sense that no WEB server is involved. However, I want the name of the server the script itself is actually running on. – Justin Noel Mar 09 '09 at 14:35
-6

One of these may have the server name:

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

Gives me the name of the server.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
ebonit
  • 13
  • 2