php server verification

2

I have a situation here where I need to identify the server where the site is located. Is it possible to check if a server is windows or linux through php code?

    
asked by anonymous 06.01.2016 / 20:57

2 answers

4

For this, PHP has a native function, and a constant:

echo php_uname();  // Sistema em execução no momento
echo PHP_OS;       // Sistema usado para BUILD do PHP

See the return format:

php_uname()                                                       PHP_OS    php-uname('s')
Linux localhost 2.4.21-0.13 #1 Fri Mar 14 15:08:06 EST 2003 i686  Linux     Linux
FreeBSD localhost 3.2-RELEASE #15: Mon Dec 17 08:46:02 GMT 2001   FreeBSD   FreeBSD
Windows NT XN1 5.1 build 2600                                     WINNT     Windows NT

Only one thing to be taken care of: PHP_OS returns which operating system PHP build was made of, and not necessarily where it's being executed.

Of course, the two things usually coincide, but knowing the difference is important at the time the thing does not work as we're expecting.

The suggestion would be this way:

$isWindows = stristr( php_uname( 's' ), 'Windows' );


Mode Parameters of php_uname() :

  • a (default) Returns in sequence the items s , n , r , v and m ;

  • % with% OS name. ex: s ;

  • FreeBSD host name. ex: n ;

  • localhost.example.com release name. ex: r ;

  • 5.1.2-RELEASE version (varies a bit);

  • v machine type. ex: m .


See working at IDEONE .

    
07.01.2016 / 04:24
1
if (stripos(php_uname('s'), 'win') === 0) {
    echo 'windows';
} else {
    echo 'other, probably *unix family';
}

or

if (DIRECTORY_SEPARATOR == '\') {
    echo 'windows';
} else {
    echo 'other, probably *unix family';
}

The first mode can generate inconsistency if a linux system whose name begins with "win" appears. Ever thought of "Winux Operating System"? It may happen, but it is a difficult thing.

The second mode is faster but in the future it may become inconsistent. For example, it may be that Windows modifies the directory separator to normal bar / or linux systems can switch to backslash \ . Although it is a very remote possibility for both cases.

In particular, I use the checking of DIRECTORY_SEPARATOR .

    
06.01.2016 / 21:50