I try to execute a bat, and in this file i try to acceed to this variable :
$_SERVER['SERVER_NAME']
But it doesn't exist this index. In fact, when i try to print_r($_SERVER)
, there is no SERVER_NAME
index. There is a reason ? Is it because i launch this function in a bat ? Have I an other solution for get this variable ?
Thank you !
The $_SERVER
variables are only available in a server environment (hosting PHP through Apache or another web server), so if you are launching it from the command line they won't be available. The parameters of this object are populated by the HTTP headers, which in your case don't exist.
If you want to ignore the $_SERVER
object when the SERVER_NAME
isn't set, you can alternative use php_uname("n")
but your mileage may vary depending on operating system.
$server_name = (isset($_SERVER['SERVER_NAME']))? // check if SERVER_NAME is set
$_SERVER['SERVER_NAME'] : // if yes, use HTTP header value
php_uname("n"); // if no, use php_uname()
The $_SERVER['SERVER_NAME']
variable holds the name of the server as supplied by the HTTP client in the Host HTTP header field. Because you do not use HTTP, this variable is not defined.
Try
echo %COMPUTERNAME%
instead if you are launching through a bat-file.