Detailed introduction to related functions about system information when PHP scripts are run

醉折花枝作酒筹
Release: 2023-03-10 11:48:02
Original
1502 people have browsed it

This article will give you a detailed introduction to the functions related to system information when the current PHP script is running. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Detailed introduction to related functions about system information when PHP scripts are run

When our PHP is executed, we can actually obtain a lot of information related to the current system. Just like many open source CMSs, they generally detect some environmental information during installation. This information can be easily and dynamically obtained.

System user related information when the script file is running

First, let’s take a look at obtaining some user information related to the current system. This user information is the system user used by our system when running php scripts.

echo '当前脚本拥有者:' . get_current_user(), PHP_EOL;
// 当前脚本拥有者:zhangyue

echo '当前脚本属组:' . getmygid(), PHP_EOL;
// 当前脚本属组:20

echo '当前脚本的用户属主:' . getmyuid(), PHP_EOL;
// 当前脚本的用户属主:501
Copy after login

Did you see it? In fact, these three functions are the corresponding file owners and groups in Linux. get_current_user() returns the user name, and getmyuid() returns the user's UID. They two correspond to the same user. getmygid() returns the user group to which the current user belongs.

Get system-related information of the currently running script

This set of functions allows us to obtain the innode information of the system, the process ID of the current script when it is running, and the service interface type. , operating system information and resource usage on which PHP is running.

echo '当前脚本的索引节点:' . getmyinode(), PHP_EOL;
// 当前脚本的索引节点:8691989143

echo '当前脚本的进程ID:' . getmypid(), PHP_EOL;
// 当前脚本的进程ID:1854
// Nginx:当前脚本的进程ID:711(php-fpm的进程ID)

echo "web服务器和PHP之间的接口类型:" . php_sapi_name(), PHP_EOL;
// web服务器和PHP之间的接口类型:cli
// Nginx:web服务器和PHP之间的接口类型:fpm-fcgi

echo "运行 PHP 的系统:" . php_uname("a"), PHP_EOL;
// 运行 PHP 的系统:Darwin zhangyuedeMBP 19.4.0 Darwin Kernel Version 19.4.0: Wed Mar  4 22:28:40 PST 2020; root:xnu-6153.101.6~15/RELEASE_X86_64 x86_64
//

echo "运行PHP的系统:" . PHP_OS, PHP_EOL;
// 运行 PHP 的系统:Darwin

// 当前脚本的资源使用情况
print_r(getrusage());
// Array
// (
//     [ru_oublock] => 0
//     [ru_inblock] => 0
//     [ru_msgsnd] => 0
//     [ru_msgrcv] => 0
//     [ru_maxrss] => 16809984
//     [ru_ixrss] => 0
//     [ru_idrss] => 0
//     [ru_minflt] => 4410
//     [ru_majflt] => 1
//     [ru_nsignals] => 0
//     [ru_nvcsw] => 0
//     [ru_nivcsw] => 86
//     [ru_nswap] => 0
//     [ru_utime.tv_usec] => 41586
//     [ru_utime.tv_sec] => 0
//     [ru_stime.tv_usec] => 41276
//     [ru_stime.tv_sec] => 0
// )
Copy after login

From the comments, we can see that getmypid() returns the process ID of the current execution when using the command line, and returns the process ID of PHP-FPM when accessed in the web page. In the same way, php_sapi_name() will also return different content according to the current running environment.

php_uname() The default parameter is 'a', which means to return complete operating system information. It also has other parameters that can return separate and different information, or when we just need to know what system we are currently operating on, it will be more convenient to use the PHP_OS constant directly.

getrusage() can return the status of system resources. For example, ru_nswap is the usage of the current swap area of ​​the system, but these parameters are not explained in detail. After all, this function is still used relatively rarely.

Get the version information of PHP and related extension components

echo "当前的PHP版本:" . phpversion(), PHP_EOL;
// 当前的PHP版本:7.3.0

echo "当前的PHP版本:" . PHP_VERSION, PHP_EOL;
// 当前的PHP版本:7.3.0

echo "当前某个扩展的版本(Swoole):" . phpversion('swoole'), PHP_EOL;
// 当前某个扩展的版本(Swoole):4.4.12

echo "当前的PHP的zend引擎版本:" . zend_version(), PHP_EOL;
// 当前的PHP的zend引擎版本:3.3.0-dev

if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
    echo '我的版本大于7.0.0,当前版本是:' . PHP_VERSION . "\n";
} else {
    echo '我的版本还在5,要赶紧升级了,当前版本是:' . PHP_VERSION . "\n";
}
Copy after login

phpversion() has the same effect as the PHP_VERSION constant without parameters, and returns the current The version number of PHP that is running. However, phpversion() can be given an extension name as a parameter, so that it can return the version number of the extension. Just like in the example, we get the version number of Swoole in the current environment. zend_version() simply returns the Zend engine version number in the current running environment.

version_compare() can help us compare version numbers conveniently. It is a comma-separated version comparison, which means that any string version number we define can be compared using it. For specific comparison rules, please refer to the official documentation.

The modification time and script running time of the current script file

echo "当前脚本文件的最后修改时间: " . date("Y-m-d H:i:s.", getlastmod()), PHP_EOL;
// 当前脚本文件的最后修改时间: 2020-06-01 08:55:49.

// nginx环境下
set_time_limit(84600);
// while(1){

// }
Copy after login

getlastmod() is very simple, it returns the last modified time of the currently running PHP file. And set_time_limit() I believe everyone is familiar with it. By default, web requests will not last for a long time before they are actively disconnected.

For example, in the php.ini file, the max_execution_time we define by default is 30 seconds. When a request consumes more than this time, the request will be disconnected. However, there are always some requests that really take us longer to execute. For example, generating documents such as Excel often takes longer.

At this time, we can use set_time_limit() to set the maximum execution time of the script to extend the execution timeout of the web request.

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/202006/source/%E5%85%B3%E4%BA%8E%E5%BD%93%E5%89%8DPHP%E8%84%9A%E6%9C%AC%E8%BF%90%E8%A1%8C%E6%97%B6%E7%B3%BB%E7%BB%9F%E4%BF%A1%E6%81%AF%E7%9B%B8%E5%85%B3%E5%87%BD%E6%95%B0.php
Copy after login

Recommended learning: php video tutorial

The above is the detailed content of Detailed introduction to related functions about system information when PHP scripts are run. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template