Php server check

Asked

Viewed 758 times

2

I have a situation here where I need to identify the server the site is located on. You can check whether a server is windows or Linux using php code?

  • 1

    phpinfo(); doesn’t show you?

  • I never saw Diego Felipe, I’ll check

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 format of the return:

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 that there is a care to be taken: the PHP_OS returns what was the operating system where the build PHP, and not necessarily where it is running.

Of course, normally the two things coincide, but knowing the difference is important at a time when it doesn’t work the way we’re hoping.

The suggestion would be this way:

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


Mode parameters of php_uname():

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

  • s name of OS. ex: FreeBSD;

  • n host name. ex: localhost.example.com;

  • r release name. ex: 5.1.2-RELEASE;

  • v version (varies a bit);

  • m type of machine. ex: i386.


See working on IDEONE.

  • Enlightening answer! Everything worked right! Vlw!

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. Have you considered "Winux Operating System"? It can happen, but it’s a difficult thing.

The second mode is faster but in the future it may become inconsistent. For example, Windows might modify 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 DIRECTORY_SEPARATOR.

  • If instead of checking if it is windows, check if it is Unix, does not reduce the margin of error of the first method?

  • 1

    It is the same and will still have a greater difficulty in finding a common identifier for "linux" or "Unix". The problem is that linux has several distributions. And many of which do not include the term "linux" or "Unix" in the name displayed. This also includes OS-X (Apple) which is also a "Unix based" system, for example. Of course you won’t find a hosting offering OS-X as a server.. was merely an example. Therefore, in the test environment, Veloper can be an Apple user. Then it would have a relevance. (within the context of your specific question, Diego Felipe)

Browser other questions tagged

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