Thursday, January 10, 2008

Detecting the OS using PHP

Sometimes it is necessary to detect the operating system on which your php code is running. PHP has number of pre defined variables and PHP_OS is one such variable which is an string containing the operating system name.
So following code checks whether the system is running on windows or not.

if (stristr(PHP_OS, 'WIN')) {
echo "Win32";
}

7 comments:

  1. What if the operating system is named "DARWIN".

    ReplyDelete
  2. DARWIN refers to macs

    ReplyDelete
  3. When I want to detect the OS, I use
    if(stristr($_SERVER['SERVER_SOFTWARE'], 'Win32'))
    $slash = '\\';
    else
    $slash = '/';

    ReplyDelete
  4. This is the easiest solution I've found on the web. I just did a if...else...to serve Mac related ads to visitors with mac

    ReplyDelete
  5. Doesn't that just tell you what software the server is running rather than the end user?

    ReplyDelete
  6. php_uname('s') is more correct that PHP_OS

    ReplyDelete