How to get information from the server operating system?

Asked

Viewed 808 times

2

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 on Linux, Windows, etc...

1 answer

3


There are a few ways you can use to get this information:

php_uname()

The php_uname() returns information about the operating system 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 check if operating system is Windows, you can do as follows:

$so = explode (" ",php_uname());

if($so[0] == "Windows"){
  //Windows
}else{
  //Não Windows
}

PHP_OS

Can use PHP_OS, a predefined constant also, where will return most of families of systems.

echo PHP_OS; //Linux

To check whether the operating system is from the Windows operating family, you can do as follows:

if(PHP_OS == "WINNT"){
        //Windows
    }elseif(PHP_OS == "Linux"){
        //Linux
    }...

Or you can still do it:

<?php
  if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    // Windows
  }else{
    // Não Windows
  }
?>

You can see a Most complete list of Wikipedia results or this question answered in our Big Brother SO.

Note

As stated in the comments there are also superglobal variables $_SERVER. Although they have tested (on a production server) one by one and only return other server-related information, (none returned the operating system) they may be useful in some other situation.

Below this script found in the documentation returning practically everything related to variables $_SERVER:

<?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>' ;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.