How can I get the operating system from my server? For example, some method to know if the operating system of the production environment is in Linux, Windows, etc ...
How can I get the operating system from my server? For example, some method to know if the operating system of the production environment is in Linux, Windows, etc ...
There are a few ways you can use this information:
The php_uname()
returns information about the operating system that PHP was built on.
echo php_uname(); //Windows NT I5 6.1 build 7601 (Windows Server 2008 R2 Enterprise Edition Service Pack 1) AMD64
To verify that the operating system is Windows, you can do the following:
$so = explode (" ",php_uname());
if($so[0] == "Windows"){
//Windows
}else{
//Não Windows
}
You can use PHP_OS
, a default constant too, where will return most of system families.
echo PHP_OS; //Linux
To verify that the operating system is in the Windows operating family, you can do the following:
if(PHP_OS == "WINNT"){
//Windows
}elseif(PHP_OS == "Linux"){
//Linux
}...
Or you can do so:
<?php
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// Windows
}else{
// Não Windows
}
?>
You can see a more complete list of results on Wikipedia or this question answered in our Great Brother SO .
As said in the comments there is also superglobal variables $ _SERVER . Although they have been tested (on a production server) one by one and they only return other information related to the server, (none returned the operating system) they may be useful in some other situation.
Below this found in documentation script that returns practically everything related to $_SERVER
variables:
<?php
$indicesServer = array('PHP_SELF',
'argv',
'argc',
'GATEWAY_INTERFACE',
'SERVER_ADDR',
'SERVER_NAME',
'SERVER_SOFTWARE',
'SERVER_PROTOCOL',
'REQUEST_METHOD',
'REQUEST_TIME',
'REQUEST_TIME_FLOAT',
'QUERY_STRING',
'DOCUMENT_ROOT',
'HTTP_ACCEPT',
'HTTP_ACCEPT_CHARSET',
'HTTP_ACCEPT_ENCODING',
'HTTP_ACCEPT_LANGUAGE',
'HTTP_CONNECTION',
'HTTP_HOST',
'HTTP_REFERER',
'HTTP_USER_AGENT',
'HTTPS',
'REMOTE_ADDR',
'REMOTE_HOST',
'REMOTE_PORT',
'REMOTE_USER',
'REDIRECT_REMOTE_USER',
'SCRIPT_FILENAME',
'SERVER_ADMIN',
'SERVER_PORT',
'SERVER_SIGNATURE',
'PATH_TRANSLATED',
'SCRIPT_NAME',
'REQUEST_URI',
'PHP_AUTH_DIGEST',
'PHP_AUTH_USER',
'PHP_AUTH_PW',
'AUTH_TYPE',
'PATH_INFO',
'ORIG_PATH_INFO') ;
echo '<table cellpadding="10">' ;
foreach ($indicesServer as $arg) {
if (isset($_SERVER[$arg])) {
echo '<tr><td>'.$arg.'</td><td>' . $_SERVER[$arg] . '</td></tr>' ;
}
else {
echo '<tr><td>'.$arg.'</td><td>-</td></tr>' ;
}
}
echo '</table>' ;