如何获得运行PHP的操作系统?

时间:2009-09-26 21:44:04

标签: php operating-system

为了构建一个unix / dos特定的脚本,我需要知道我是哪种操作系统。

我如何获得这些信息?
phpinfo();告诉我更多,而且不太清楚我是否在unix上运行。

10 个答案:

答案 0 :(得分:92)

PHP有很多predefined constants通常很有用。

此处,您正在寻找 PHP_OS


例如,在我当前的机器上,这段代码:

var_dump(PHP_OS);

给予:

string 'Linux' (length=5)


您有一些示例,并与 php_uname 函数可以使您manual page of php_uname的内容进行比较;例如(引用)

<?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!';
}

那页也说:

  

仅供操作的名称   系统,考虑使用PHP_OS   不变,但请记住这一点   常数将包含操作   系统PHP 内置

答案 1 :(得分:31)

请记住,PHP_OS实际上包含构建PHP的平台。这可能与部署它的平台不同。因此php_uname('s')更可靠。

答案 2 :(得分:8)

PHP 7.2.0 开始,我们有一个新的预定义常量来获取操作系统系列,即 PHP_OS_FAMILY 。 它返回字符串 &#39; Windows&#39; BSD&#39;&#39; OSX&#39;,&#39; Solaris&#39; Linux&#39; Linux&#39;或者&#39;未知&#39;。

答案 3 :(得分:3)

PHP_OS预先定义了主机操作系统名称:http://us2.php.net/manual/en/reserved.constants.php

答案 4 :(得分:2)

有两种不同的方法可以检查运行PHP的平台。

  1. 使用PHP_OS这是一个const并将指向'操作 系统名称'你的PHP是在其中构建的。
  2. 使用PHP内置函数php_uname(),它将告诉您更多信息 关于平台(操作系统名称,主机名,版本 脚本运行的信息,版本名称,机器类型 在它上面。

答案 5 :(得分:2)

PHP不提供获取分发名称的任何函数,php_uname类似于Linux命令uname,不提供有关分发本身的任何信息。

php_unamePHP_OS都没有提供足够的信息。关于分发但是OS类型(例如Linux / Windows)。

我认为了解正在运行的操作系统/发行版的最佳方法是阅读/etc/os-release,好消息是这个文件对所有系统用户都有读取权限,不好的是它可能无法在共享上运行托管。

这里我写了一个非常简单的PHP函数,它读取os-release并将其转换为数组:

    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);
    }

此功能打印如下:

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]丹麦语意味着好运。

答案 6 :(得分:1)

在php 7.2.0上,您可以使用PHP_OS_FAMILY常量:

在其他PHP版本中,您可以使用:

/**
* return DOS OR UNIX
*/
function familyOS() {
   return (stripos(PHP_OS, "WIN") === 0)? "DOS" : "UNIX";
}

答案 7 :(得分:0)

这是我正在使用的

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;
}

我们也可以使用PHP_OS,但这并不准确,如文档所述 PHP的构建操作系统

所以php_uname('s')是个好方法。

但是PhpStrom 2020.2 IDE建议使用 [EA] PHP_OS常量。

答案 8 :(得分:-1)

#!/usr/bin/env php
<?php
$platform = DIRECTORY_SEPARATOR === '\\'
    ? 'Windows'
    : 'Unix/Linux';

我知道这不是很精细,但是对于Win和* nix系统之间的简单识别就足够了。 YMMV

答案 9 :(得分:-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);
相关问题