As we execute PHP, we can actually get a lot of information about the current system. This information can be easily accessed dynamically, just as many open source CMS detect some environment information during installation.

Information about the system user when the script file is running

First, let’s look at getting some user information relevant to the current system. This user information is the system user that our system uses to run the PHP script.

echo 'Current script owner:' . get_current_user(), PHP_EOL;
// Current script owner: Zhangyue

echo 'Current script Group:' . getmygid(), PHP_EOL;
// Current script group: 20

echo 'User owner of current script:' . getmyuid(), PHP_EOL;
// User owner of the current script: 501
Copy the code

See? Get_current_user () returns the user name, getMyuid () returns the user’s UID, and they are the same user. Getmygid () returns the group to which the current user belongs.

Obtain information about the system where the script is running

This set of functions lets you get the system’s InNode information, the process ID when the script is currently running, the service interface type, the operating system on which PHP is running, and resource usage.

echo 'Index node of current script:' . getmyinode(), PHP_EOL;
// Index node of the current script: 8691989143

echo 'Process ID of current script:' . getmypid(), PHP_EOL;
// Process ID of the current script: 1854
// Nginx: process ID of current script: 711 (php-fpm process ID)

echo Type of interface between the Web server and PHP: . php_sapi_name(), PHP_EOL;
// The interface type between the Web server and PHP is cli
// Nginx: interface type between the Web server and PHP: fpm-fcgi

echo "System running PHP:" . php_uname("a"), PHP_EOL;
// 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 "System running PHP:" . PHP_OS, PHP_EOL;
// The system running PHP: Darwin

// Resource usage of the current script
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 the code

As you can see from the comment, getmypId () returns the current executing process ID when using the command line and the php-fpm process ID when accessing the web page. Similarly, php_sapi_name() returns different values depending on the environment in which it is currently running.

Php_uname () the default argument is ‘a’, which means to return complete operating system information. It also has other parameters that can return separate and different information, or it is more convenient to use the PHP_OS constant when we just need to know what system we are operating on.

Getrusage () returns the usage of system resources, such as ru_nswap, which is the current swap usage of the system, but these parameters are not specified because this function is rarely used.

Get version information for PHP and related extension components

echo "Current PHP version:" . phpversion(), PHP_EOL;
// Current PHP version: 7.3.0

echo "Current PHP version:" . PHP_VERSION, PHP_EOL;
// Current PHP version: 7.3.0

echo "The current version of an extension (Swoole) :" . phpversion('swoole'), PHP_EOL;
// The current version of an extension (Swoole) is 4.4.12

echo "Current zend engine version of PHP:" . zend_version(), PHP_EOL;
// The current PHP zend engine version is 3.3.0-dev

if (version_compare(PHP_VERSION, '7.0.0') > =0) {
    echo 'My version is greater than 7.0.0, the current version is:' . PHP_VERSION . "\n";
} else {
    echo 'My version is still 5, need to upgrade, the current version is:' . PHP_VERSION . "\n";
}
Copy the code

Phpversion () has the same effect as the PHP_VERSION constant without an argument, returning the version number of the currently running PHP. However, phpVersion () can be given an extension name, so that it can return the version number of the extension. Like in the example, we get the version number of Swoole in the current context. Zend_version () simply returns the version number of the Zend engine in the current running environment.

Version_compare () helps us easily compare version numbers. It is a comma-separated version comparison, that is, we can use it to compare our own string version numbers. You can refer to the official documentation for specific comparison rules.

The current script file modification time and script running time

echo "Last modified time of current script file:" . date("Y-m-d H:i:s.", getlastmod()), PHP_EOL;
// Last modified time of the current script file: 2020-06-01 08:55:49.

// In the nginx environment
set_time_limit(84600);
// while(1){

// }
Copy the code

Getlastmod () is very simple and simply returns the last time the currently running PHP file was modified. Set_time_limit () is no stranger. By default, Web requests don’t last long enough to be voluntarily disconnected. For example, in the php.ini file, max_execution_time is defined as 30 seconds by default, and when a request is consumed for more than 30 seconds, the request is disconnected. However, there will always be requests that take longer to execute, such as generating Excel documents. At this point, we can use set_time_limit() to set the maximum script execution time to extend the execution timeout for web requests.

Test code:

Github.com/zhangyue050…

Reference Documents:

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…

www.php.net/manual/zh/f…