操作系统-如何获取运行PHP的操作系统?
为了构建unix / dos特定的脚本,我需要知道我使用的是哪种操作系统。
我如何获得此信息?
phpinfo();告诉我更多信息,但不清楚我是否在Unix上运行。
9个解决方案
94 votes
PHP具有许多经常有用的预定义常量。
在这里,php_uname是您要寻找的。
例如,在我当前的计算机上,此代码:
var_dump(PHP_OS);
给出:
string 'Linux' (length=5)
您可以在PHP_OS的手册页上找到一些示例和与php_uname函数可以得到的功能进行比较; 例如(引用):
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建立在其上。
Pascal MARTIN answered 2020-02-06T00:03:46Z
31 votes
请记住,PHP_OS实际上包含构建PHP的平台。 该平台可能与部署平台的平台不同。 因此php_uname('s')更可靠。
spikyjt answered 2020-02-06T00:04:06Z
8 votes
从PHP 7.2.0开始,我们有了一个新的预定义常量来获取操作系统系列,即PHP_OS_FAMILY。它返回一个字符串“ Windows”,“ BSD”,“ OSX”,“ Solaris”,“ Linux”或“未知”。
shery089 answered 2020-02-06T00:04:26Z
3 votes
PHP_OS使用主机操作系统名称预先定义:[http://us2.php.net/manual/zh/reserved.constants.php]
jmucchiello answered 2020-02-06T00:04:46Z
2 votes
有两种方法可以检查平台在其上运行的PHP。
使用php_uname()这是一个const,并将指向“系统名称”,即您的PHP内置在其中。
使用PHP内置函数php_uname(),它将告诉您更多信息关于平台(操作系统名称,主机名,版本信息,发行名称,计算机类型),您的脚本正在运行在上面。
M.J.Ahmadi answered 2020-02-06T00:05:16Z
2 votes
PHP不提供任何功能来获取发行版的名称,os-release与Linux命令PHP_OS类似,不提供有关发行版本身的任何信息。
2734851147008008836608和PHP_OS均未提供足够的信息。 有关发行版但操作系统类型(例如Linux / Windows)的信息。
我认为了解正在运行的OS /发行版的最佳方法是读取2734851147008008836608,好消息是此文件对所有系统用户都具有读取权限,而坏消息是它可能无法在共享主机上工作。
在这里,我编写了一个非常简单的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
)
举行og lykke [1] ;-)
[1]丹麦语意味着好运。
Ahmad answered 2020-02-06T00:06:03Z
1 votes
在PHP 7.2.0上,您可以使用PHP_OS_FAMILY常量:
[http://php.net/manual/en/reserved.constants.php#constant.php-os-family]
在其他PHP版本中,可以使用:
/**
* return DOS OR UNIX
*/
function familyOS() {
return (stripos(PHP_OS, "WIN") === 0)? "DOS" : "UNIX";
}
fitorec answered 2020-02-06T00:06:32Z
-1 votes
#!/usr/bin/env php
$platform = DIRECTORY_SEPARATOR === '\\'
? 'Windows'
: 'Unix/Linux';
我知道这不是很精细,但是对于Win和* nix系统之间的简单识别就足够了。 青年汽车
helvete answered 2020-02-06T00:06:52Z
-4 votes
$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 = "Operating System: ".$user_os."";
print_r($device_details);
rahul answered 2020-02-06T00:07:07Z