Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Server name from the command line

Tags:

php

Apparently $_SERVER['SERVER_NAME'] is set only when executing the php script from the browser.

Is there any way to know the server's name from the command line ?

I tried php_uname("n") as proposed in this discussion PHP Server Name from Command Line. But it doesn't work for me, it returns my username instead of the server's name.

like image 426
Ayoub Omari Avatar asked Nov 27 '25 18:11

Ayoub Omari


1 Answers

There is no "server" involved when executing a script from the command line. You're just running a program on a machine. That machine isn't per se associated with any domain name. One machine can run multiple web servers which are each responsible for several domains. So $_SERVER['SERVER_NAME'], which comes from the web server's configuration (e.g. Apache's ServerName directive), only makes sense in the context of an incoming HTTP request.

You'll need to manually configure/tell your script what domain it should work on, it can't detect it without context.

like image 102
deceze Avatar answered Nov 29 '25 07:11

deceze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!