75

For building a unix/dos specific script I need to know on which kind of operating system I am.

How do i get this information?
phpinfo(); tells me a lot more and not very clear whether I'm running on unix or not.

Martin Klepsch
  • 1,875
  • 3
  • 18
  • 24

10 Answers10

127

PHP has many predefined constants that are often useful.

Here, PHP_OS is the one you are looking for.


For instance, on my current machine, this code :

var_dump(PHP_OS);

Gives :

string 'Linux' (length=5)


You have some examples and comparisons with what the php_uname function can get you on the manual page of php_uname ; for instance (quoting) :

<?php
echo php_uname();
echo PHP_OS;

/* Some possible outputs:
Linux localhost 2.4.21-0.13mdk #1 Fri Mar 14 15:08:06 EST 2003 i686
Linux

FreeBSD localhost 3.2-RELEASE #15: Mon Dec 17 08:46:02 GMT 2001
FreeBSD

Windows NT XN1 5.1 build 2600
WINNT
*/

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    echo 'This is a server using Windows!';
} else {
    echo 'This is a server not using Windows!';
}

That page also says :

For the name of just the operating system, consider using the PHP_OS constant, but keep in mind this constant will contain the operating system PHP was built on.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 6
    How could PHP_OS ever "worked quite good" if it gives you the OS PHP was built on? It's bound to break sooner or later. – Pacerier Dec 12 '14 at 11:32
  • This is the wrong answer! See spikyjt's answer below! – undo Jan 22 '20 at 06:13
  • @Pacerier The present php documentation says `"The operating system PHP was built for."`, so unless someone is using a php version meant for windows on linux, it should do fine. On a side note, for php7.2 and above, `PHP_OS_FAMILY` as shown in shery089's answer should suffice for most users – Arun A S Feb 25 '21 at 14:42
47

Just bear in mind that PHP_OS actually contains the platform on which PHP was built. This may not be the same platform as that on which it is deployed. Therefore php_uname('s') is more reliable.

spikyjt
  • 2,210
  • 21
  • 16
  • `php_uname('s')` on Windows 7 returns "Windows NT" so I don't know how much more reliable it is :) I guess one could argue Win 7 is based on NT but still... – laurent Dec 31 '13 at 05:53
  • 10
    Windows 7 absolutely is Windows NT (version 6.1). Vista was Windows NT 6, XP was NT 5.1, 2000 was NT 5. So yes it is reliable! Check [here](http://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions) for reference. Type `ver` at a cmd prompt to see the version number of your Win OS. – spikyjt Jan 06 '14 at 00:28
  • 2
    This seems like the correct answer to me. The accepted answer is flat-out wrong and does not answer the OPs question. – Evan de la Cruz Apr 21 '16 at 21:51
  • Could you please elaborate what "The operating system PHP was built for." means? (the description of `PHP_OS` at http://php.net/manual/en/reserved.constants.php) Is it like an OS where the PHP interpreter were compiled? How can it be used on another OS? (this is possible for similar Unix systems? for different versions of the same OS?) – YakovL Feb 01 '18 at 17:34
  • 1
    @YakovL the wording you mention is not in my answer. My answer refers to "the platform on which PHP was built" and "the platform on which it[PHP] is deployed". Whilst this is very rarely a different operating system, it is usually a different machine and it is technically possible to cross-compile for other platforms. To be clear it is important to understand that PHP_OS is a compiled in constant, referring to the build of PHP itself and has nothing to do with the host server. php_uname() is a runtime function that does return information about the host server. – spikyjt Feb 02 '18 at 13:42
  • I understand, I just thought you may know some particular examples when the difference takes place – YakovL Feb 02 '18 at 13:46
23

As of PHP 7.2.0 we have a new Predefined Constant to get the operating system family i.e. PHP_OS_FAMILY. It returns a string Either of 'Windows', 'BSD', 'OSX', 'Solaris', 'Linux' or 'Unknown'.

shery089
  • 702
  • 11
  • 19
  • This doesn't tell you the operating system you are on. It returns the operating system PHP was built for. I reckon that there's a 99% chance of them being the same, but it would be possible for a script to use a PHP version on another OS. – dewd Dec 16 '22 at 15:50
  • Careful guys, there was only one pre-release version: [7.2.0alpha1](https://github.com/php/php-src/blob/php-7.2.0alpha1/main/php.h#L50) that returned "OSX" for Mac. This was discussed in the [internals](https://externals.io/message/99501) and changed in [7.2.0alpha2](https://github.com/php/php-src/blob/php-7.2.0alpha2/main/php.h#L50). It consistently returns "Darwin" for Mac in all stable releases PHP >= [7.2.0](https://github.com/php/php-src/blob/php-7.2.0/main/php.h#L50). – Ultimater Jan 27 '23 at 11:51
7

PHP Does not provide any function to get the name of the distribution, php_uname is similar to Linux command uname, does not provide any info about the distribution itself.

Neither php_uname nor PHP_OS give sufficient info. about the distribution but the OS type (e.g. Linux / Windows).

I think the best way to know what is the running OS/distribution is to read /etc/os-release, the good thing is this file has read permission for all system users and the bad thing is it may not work on shared hosting.

Here I wrote a very simple PHP function which reads and convert os-release to an array:

    function getOSInformation()
    {
        if (false == function_exists("shell_exec") || false == is_readable("/etc/os-release")) {
            return null;
        }

        $os         = shell_exec('cat /etc/os-release');
        $listIds    = preg_match_all('/.*=/', $os, $matchListIds);
        $listIds    = $matchListIds[0];

        $listVal    = preg_match_all('/=.*/', $os, $matchListVal);
        $listVal    = $matchListVal[0];

        array_walk($listIds, function(&$v, $k){
            $v = strtolower(str_replace('=', '', $v));
        });

        array_walk($listVal, function(&$v, $k){
            $v = preg_replace('/=|"/', '', $v);
        });

        return array_combine($listIds, $listVal);
    }

This function prints something like this:

Array
(
    [name] => Ubuntu
    [version] => 16.04.2 LTS (Xenial Xerus)
    [id] => ubuntu
    [id_like] => debian
    [pretty_name] => Ubuntu 16.04.2 LTS
    [version_id] => 16.04
    [home_url] => http://www.ubuntu.com/
    [support_url] => http://help.ubuntu.com/
    [bug_report_url] => http://bugs.launchpad.net/ubuntu/
    [version_codename] => xenial
    [ubuntu_codename] => xenial
)

Held og lykke [1] ;-)

[1] Danish phrase means good luck.

Ahmad
  • 808
  • 9
  • 14
4

On php 7.2.0 you can use the PHP_OS_FAMILY constant:

In other PHP version you can use:

/**
* return DOS OR UNIX
*/
function familyOS() {
   return (stripos(PHP_OS, "WIN") === 0)? "DOS" : "UNIX";
}
fitorec
  • 4,257
  • 2
  • 24
  • 18
3

PHP_OS is prefined with the host os name: http://us2.php.net/manual/en/reserved.constants.php

jmucchiello
  • 18,754
  • 7
  • 41
  • 61
2

There are 2 different way to check the platform that your PHP is running on it.

  1. Using PHP_OS which is a const and will point to the 'operating system name' that your PHP was built in it.
  2. Using PHP built in function php_uname() that will tell you more about platform (Operating system name, Host name, Version information, Release name, Machine type) that your script is running on it.
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
M.J.Ahmadi
  • 3,931
  • 4
  • 17
  • 24
1
#!/usr/bin/env php
<?php
$platform = DIRECTORY_SEPARATOR === '\\'
    ? 'Windows'
    : 'Unix/Linux';

I am aware that this is not very granular, but it may suffice for a simple recognition between Win and *nix systems. YMMV

helvete
  • 2,455
  • 13
  • 33
  • 37
1

Here what i am using

switch (strtolower(php_uname('s'))) {
    case "linux": echo "Server is running on linux OS"
        break;
    case "windows nt": echo "Server is running on Windows OS"
        break;
    default:
        echo "Unknown";
        break;
}

We can also use PHP_OS but this is not accurate as documentation says The operating system PHP was built for

so php_uname('s') is good approach.

However PhpStrom 2020.2 IDE suggests [EA] PHP_OS constant should be used instead.

dipenparmar12
  • 3,042
  • 1
  • 29
  • 39
-4
$user_agent     =   $_SERVER['HTTP_USER_AGENT'];
function getOS() { 

    global $user_agent;

    $os_platform    =   "Unknown OS Platform";

    $os_array       =   array(
                            '/windows nt 6.2/i'     =>  'Windows 8',
                            '/windows nt 6.1/i'     =>  'Windows 7',
                            '/windows nt 6.0/i'     =>  'Windows Vista',
                            '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
                            '/windows nt 5.1/i'     =>  'Windows XP',
                            '/windows xp/i'         =>  'Windows XP',
                            '/windows nt 5.0/i'     =>  'Windows 2000',
                            '/windows me/i'         =>  'Windows ME',
                            '/win98/i'              =>  'Windows 98',
                            '/win95/i'              =>  'Windows 95',
                            '/win16/i'              =>  'Windows 3.11',
                            '/macintosh|mac os x/i' =>  'Mac OS X',
                            '/mac_powerpc/i'        =>  'Mac OS 9',
                            '/linux/i'              =>  'Linux',
                            '/ubuntu/i'             =>  'Ubuntu',
                            '/iphone/i'             =>  'iPhone',
                            '/ipod/i'               =>  'iPod',
                            '/ipad/i'               =>  'iPad',
                            '/android/i'            =>  'Android',
                            '/blackberry/i'         =>  'BlackBerry',
                            '/webos/i'              =>  'Mobile'
                        );

    foreach ($os_array as $regex => $value) { 

        if (preg_match($regex, $user_agent)) {
            $os_platform    =   $value;
        }

    }   

    return $os_platform;

}

$user_os  =   getOS();


$device_details =   "<strong>Operating System: </strong>".$user_os."";

print_r($device_details);
the_unknown_spirit
  • 2,518
  • 7
  • 34
  • 56
rahul
  • 9
  • 1
    Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written – WhatsThePoint Nov 26 '18 at 08:25
  • This gets the client operating system (or makes a best guess at it), not the server operating system, and as such does not answer the question. It should also be noted that the client user agent can be spoofed easily, and this is not reliable even in cases where you do want the clients os. This is also only relevant when an http request is made, and is useless from the command line. – mopsyd Mar 28 '19 at 20:17