php_uname

(PHP 4 >= 4.0.2, PHP 5, PHP 7, PHP 8)

php_uname返回运行 PHP 的系统的有关信息

说明

php_uname ( string $mode = "a" ) : string

php_uname() 返回了运行 PHP 的操作系统的描述。 这和 phpinfo() 最顶端上输出的是同一个字符串。 如果仅仅要获取操作系统的名称。可以考虑使用常量 PHP_OS,不过要注意该常量会包含 PHP 构建(built)时的操作系统名。

在一些旧的 UNIX 平台,它有可能无法检测到当前系统的信息,然后会还原显示成构建 PHP 时的系统信息。 这仅仅在你的 uname() 函数库不存在或无法运行时发生。

参数

mode

mode 是单个字符,用于定义要返回什么信息:

  • 'a':此为默认。包含序列 "s n r v m" 里的所有模式。
  • 's':操作系统名称。例如: FreeBSD
  • 'n':主机名。例如: localhost.example.com
  • 'r':版本名称,例如: 5.1.2-RELEASE
  • 'v':版本信息。操作系统之间有很大的不同。
  • 'm':机器类型。例如:i386

返回值

返回描述字符串。

范例

Example #1 一些 php_uname() 的例子

<?php
echo php_uname();
echo 
PHP_OS;

/* 比如有些会输出:
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_OS03)) === 'WIN') {
    echo 
'This is a server using Windows!';
} else {
    echo 
'This is a server not using Windows!';
}

?>

同样可以方便地使用一些相关的 PHP 预定义常量,例如:

Example #2 一些系统相关常量的例子

<?php
// *nix
echo DIRECTORY_SEPARATOR// /
echo PHP_SHLIB_SUFFIX;    // so
echo PATH_SEPARATOR;      // :

// Win*
echo DIRECTORY_SEPARATOR// \
echo PHP_SHLIB_SUFFIX;    // dll
echo PATH_SEPARATOR;      // ;
?>

参见

User Contributed Notes

pyst 30-Sep-2020 01:05
Note that the argument is not a "format". This means that <?php uname('a') ?> is not equivalent to <?php uname('s n r v m') ?>, but rather <?php uname('s') . ' ' . uname('n') . ' ' . uname('r') . ' ' . uname('v') . ' ' . uname('m') ?>.
Ricardo Striquer (ricardophp yohoocombr) 18-Sep-2019 05:24
Although it does not indicate receiving parameters this function allows some chars as parameters, they are the options a; m; n; r; s; v.

I don't know exactly what each one does, but below is a code example and its output on PHP 5.5 running on RedHat 4.4

<?php
echo '<pre>';
echo
'(void): '.php_uname()."\n"; // output: "(void): Linux web989.uni5.net 4.4.180 #1 SMP Wed May 22 15:27:37 -03 2019 x86_64"
echo 'a: '.php_uname('a')."\n"; // output: "a: Linux web989.uni5.net 4.4.180 #1 SMP Wed May 22 15:27:37 -03 2019 x86_64"
echo 'm: '.php_uname('m')."\n"; // output: "m: x86_64"
echo 'n: '.php_uname('n')."\n"; // output: "n: web989.uni5.net"
echo 'r: '.php_uname('r')."\n"; // output: "r: 4.4.180"
echo 's: '.php_uname('s')."\n"; // output: "s: Linux"
echo 'v: '.php_uname('v')."\n"; // output: "v: #1 SMP Wed May 22 15:27:37 -03 2019"
// echo file_get_contents('/etc/issue')."\n";

echo '</pre>';
phpinfo();
?>

Note that I used file_get_contentes with a file, that file is present in most linux boxies and varies on content, but most of times it indicates kernel version, linux version, distribution and so on. In my case my user didn't have access to the file, so to identify that it was a red hat I paid attention to php_info outputs, there in the middle was the postgress driver description, p. eg indicating the distribution.

I guess there is no easy way to find out the server version programmatically a 100% of times, but by and large you can get around by yourself.
Grzechooo+php at gmail dot com 29-Sep-2012 05:07
Note that PHP won't tell you that it reverted to displaying platform it was built on.
Yzmir Ramirez 24-Aug-2011 10:00
Note that php_uname('n') does not always equal $_SERVER['HOST_NAME']

The machine that you are running the script may server many different host names so don't use this when building urls.