Hướng dẫn dùng uname linux trong PHP

Trên Linux, bạn có thể sử dụng lệnh uname để xem các thông tin này. Mặc dù bạn có thể tìm thấy tất cả những chi tiết đó trong các tập tin tương ứng bằng cách dùng proc, nhưng uname dễ sử dụng và nhanh chóng hơn.

Show

Hướng dẫn dùng uname linux trong PHP

Cách dùng cơ bản lệnh như sau:

Ngoài ra, có thể xem qua các ví dụ sau để hiểu rõ hơn công dụng của lệnh này.

1. Không có Option nào:

Bạn sẽ thấy được thông tin Kernel, ngoài ra cũng có thể sử dụng option -s để có thông tin tương tự:

2. Xem thông tin hostname của máy chủ: -n

Kết quả của lệnh này sẽ giống kết quả lệnh hostname.

3. Thông tin phiên bản Kernel: -r

2 2.6.32-100.28.5.el6.x86_64

4. Thông tin phát hành Kernel: -v

2 #1 SMP Wed Feb 2 18:40:23 EST 2011

5. Thông tin phần cứng:

Chỉ ra phần cứng 64bit hay 32bit bằng cách dùng lệnh bên trên.

6. Tên hệ điều hành: -o

7. Lấy cả thông tin: -a

2 Linux dev-server 2.6.32-100.28.5.el6.x86_64 #1 SMP Wed Feb 2 18:40:23 EST 2011 x86_64 x86_64 x86_64 GNU/Linux

Chỉ với option -a, ta có thể thể hiện ra màn hình tất cả thông tin của máy chủ, phục vụ tốt cho việc vận hành, theo dõi. Đặc biệt quan trọng khi bạn đang cân nhắc để cài đặt, nâng cấp hay tích hợp thêm các ứng dụng lên server.

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

php_unameReturns information about the operating system PHP is running on

Description

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

On some older UNIX platforms, it may not be able to determine the current OS information in which case it will revert to displaying the OS PHP was built on. This will only happen if your uname() library call either doesn't exist or doesn't work.

Parameters

mode

mode is a single character that defines what information is returned:

  • 'a': This is the default. Contains all modes in the sequence "s n r v m".
  • 's': Operating system name. eg. FreeBSD.
  • 'n': Host name. eg. localhost.example.com.
  • 'r': Release name. eg. 5.1.2-RELEASE.
  • 'v': Version information. Varies a lot between operating systems.
  • 'm': Machine type. eg. i386.

Return Values

Returns the description, as a string.

Examples

Example #1 Some php_uname() examples

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_OS03)) === 'WIN') {
    echo 
'This is a server using Windows!';
} else {
    echo 
'This is a server not using Windows!';
}
?>

There are also some related Predefined PHP constants that may come in handy, for example:

Example #2 A few OS related constant examples

// *nix
echo DIRECTORY_SEPARATOR// /
echo PHP_SHLIB_SUFFIX;    // so
echo PATH_SEPARATOR;      // :

// Win*

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

See Also

  • phpversion() - Gets the current PHP version
  • php_sapi_name() - Returns the type of interface between web server and PHP
  • phpinfo() - Outputs information about PHP's configuration

pyst

1 year ago

Note that the argument is not a "format". This means that ('a') ?> is not equivalent to ('s n r v m') ?>, but rather ('s') . ' ' . uname('n') . ' ' . uname('r') . ' ' . uname('v') . ' ' . uname('m') ?>.

Ricardo Striquer (ricardophp yohoocombr)

2 years ago

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

echo '

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

Yzmir Ramirez

11 years ago

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.