How to get the OS on which PHP is running?

2020-01-25 06:36发布

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.

9条回答
beautiful°
2楼-- · 2020-01-25 07:08
#!/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

查看更多
Fickle 薄情
3楼-- · 2020-01-25 07:14

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.
查看更多
淡お忘
4楼-- · 2020-01-25 07:15

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

查看更多
叛逆
5楼-- · 2020-01-25 07:15
$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);
查看更多
▲ chillily
6楼-- · 2020-01-25 07:17

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'.

查看更多
啃猪蹄的小仙女
7楼-- · 2020-01-25 07:17

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.

查看更多
登录 后发表回答