Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does PHP get the system-related information of the currently running script?

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "PHP how to get the system-related information of the current running script", the content of the explanation in the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "PHP how to get the system-related information of the current running script"!

About the current PHP script runtime system information related functions

When our PHP is executed, we can actually get a lot of information about the current system. Just like many open source CMS generally detect some environmental information at installation time, this information can be easily obtained dynamically.

System user information when the script file is running

First of all, let's take a look at getting some user information about 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: zhangyueecho 'current script belongs to group:'. Getmygid (), PHP_EOL;// current script group: 20echo 'current script user owner:'. Getmyuid (), the user owner of PHP_EOL;// 's current script: 501

Can you see that? In fact, these three functions are the file owner and group in the corresponding Linux. Get_current_user () returns the user name, and getmyuid () returns the user's UID, both of which are the same user. Getmygid () returns the user group to which the current user belongs.

Get information about the system that is currently running the script

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

Echo 'index node of the current script:'. Getmyinode (), the index node of the PHP_EOL;// current script: 8691989143echo 'the process ID:' of the current script. Getmypid (), PHP_EOL;// current script process ID:1854// Nginx: current script process ID:711 (php-fpm process ID) echo "interface type between web server and PHP:". Php_sapi_name (), interface type between PHP_EOL;// web server and PHP: interface type between cli// Nginx:web server and PHP: fpm-fcgiecho "system running PHP:. Php_uname ("a"), PHP_EOL;// system running 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 "system running PHP:". PHP_OS, PHP_EOL;// system running PHP: resource usage of Darwin// 's 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] = > 0amp / [ru_nvcsw] = > 0max / [ru_nivcsw] = > 86max / [ru_nswap] = > 0max / [ru_utime.tv_usec] = > 41586max / [ru_utime.tv_sec] = > 0ram / [ru_stime.tv_usec] = > 41276amp / [ru_stime.tv_sec] = > 0max /)

From the comments, we can see that getmypid () returns the currently executed process ID when using the command line and PHP-FPM 's process ID when visited in the web page. Similarly, php_sapi_name () returns different content depending on the environment in which it is running.

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

Getrusage () can return system resources, such as ru_nswap is the use of the system's current swap exchange, but these parameters are not very detailed, after all, this function is still less used.

Get version information of PHP and related extension components echo "current PHP version:." Phpversion (), current PHP version of PHP_EOL;//: 7.3.0echo current PHP version:. PHP_VERSION, current PHP version of PHP_EOL;//: 7.3.0echo "current extended version (Swoole):". Phpversion ('swoole'), the current extended version of PHP_EOL;// (Swoole): 4.4.12echo "current zend engine version of PHP:". Zend_version (), the zend engine version of PHP_EOL;// 's current PHP: 3.3.0-devif (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 in 5, I need to upgrade quickly, the current version is:'. PHP_VERSION. "\ n";}

Phpversion () has the same effect as the PHP_VERSION constant without arguments, returning the version number of the currently running PHP, but phpversion () can give an argument to the extension component name so that it can return the version number of the extension component. As in the example, we get the version number of the Swoole in the current environment. Zend_version () simply returns the Zend engine version number in the current runtime environment.

Version_compare () can help us compare version numbers easily. It is a comma-separated version comparison, that is, we can use it to compare string version numbers we define ourselves. For specific comparison rules, please refer to the official documentation.

Current script file modification time and script run time echo "current script file last modification time:". Date ("Y-m-d H:i:s.", getlastmod ()), the last modification time of the current script file of PHP_EOL;//: 2020-06-01 08WR 55RX 49.max / set_time_limit (84600) in nginx environment; / / while (1) {/ /}

Getlastmod () is simple enough to return the time when the currently running PHP file was last modified. And set_time_limit () believes that everyone is no stranger. By default, web requests do not last long before they are actively disconnected. For example, in the php.ini file, we define the max_execution_time as 30 seconds by default. When a request is consumed for more than this time, the request will be disconnected. However, there will always be requests that do take us longer to execute, such as generating documents such as Excel. At this point, we can use set_time_limit () to set the maximum script execution time to extend the execution timeout of the web request.

Thank you for your reading, the above is the content of "PHP how to get the system-related information of the current running script". After the study of this article, I believe you have a deeper understanding of how PHP gets the system-related information of the current running script, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report