PHP内核-用户请求的开始跟结束
PHP内核-用户请求的开始和结束
安装过apache的人都知道,我们安装完PHP后,只是对apache进行配置,主要是添加mod_php5.so这个扩展,然后把apache重新启动,就可以正常使用PHP,这过程中我们从来没有手动启动过PHP的相关进程,那它是如何启动的呢?
它是随着apache的启动而启动的,安装在服务器上的PHP程序通过mod_php5.so模块和apache进行通信,其实在我前一篇博客里,我们知道,这个模块本质上是SAPI。在这篇博文我将讨论一次用户请求的过程,主要是如何发生通信的。
PHP主要包括三个模块,分别是内核部分,Zend引擎以及扩展部分。
内核主要用来处理请求、文件流、错误处理等相关操作;
Zend引擎(ZE)用以将源文件转换成机器语言,然后在虚拟机上运行它。
扩展层是一组函数、类库和流。PHP使用扩展层来执行一些特定的操作。
当一个请求到达之后,PHP的启动分为两个阶段:
- 第一个阶段是随apache的启动而启动的,也就是启动PHP模块,这个简单的过程在博文"Apache的PHP模块启动"有讨论。这个阶段生成一些环境变量,这些环境变量在SAPI的整个生命周期内都是可见的。
- 第二个阶段是一个请求到来之后,生成一些和请求相关的环境和变量。
在第二阶段,当一个页面请求到来时,SAPI层(APACHE)将控制权交给PHP层,此时PHP设置用于处理本次请求的环境变量。随后PHP调用各个模块的RINT方法,对请求进行初始化。前提是这些模块都在php.ini文件中有配置。如mysql模块的请求初始化:
PHP_RINIT_FUNCTION(mysql) { #if defined(ZTS) && MYSQL_VERSION_ID >= 40000 if (mysql_thread_init()) { return FAILURE; } #endif MySG(default_link)=-1; MySG(num_links) = MySG(num_persistent); /* Reset connect error/errno on every request */ MySG(connect_error) = NULL; MySG(connect_errno) =0; MySG(result_allocated) = 0; return SUCCESS; }
PHP_RINIT_FUNCTION(session) { php_rinit_session_globals(TSRMLS_C); if (PS(mod) == NULL) { char *value; value = zend_ini_string("session.save_handler", sizeof("session.save_handler"), 0); if (value) { PS(mod) = _php_find_ps_module(value TSRMLS_CC); } if (!PS(mod)) { /* current status is unusable */ PS(session_status) = php_session_disabled; return SUCCESS; } } if (PS(serializer) == NULL) { char *value; value = zend_ini_string("session.serialize_handler", sizeof("session.serialize_handler"), 0); if (value) { PS(serializer) = _php_find_ps_serializer(value TSRMLS_CC); } } if (PS(mod) == NULL || PS(serializer) == NULL) { /* current status is unusable */ PS(session_status) = php_session_disabled; return SUCCESS; } if (PS(auto_start)) { php_session_start(TSRMLS_C); } return SUCCESS; }
在PHP关闭的时候,也分两个阶段,
- 第一阶段: 在一个页面结束的时候,会按照顺序调用各个模块的PHP_RSHUTDOWN_FUNCTION方法,清除所产生的变量和符号。如
PHP_RSHUTDOWN_FUNCTION(session) { php_session_flush(TSRMLS_C); php_rshutdown_session_globals(TSRMLS_C); return SUCCESS; }
- 第二阶段:最后,所有的请求都已处理完毕,SAPI也准备关闭了,PHP开始执行第二步:PHP调用每个扩展的MSHUTDOWN方法,这是各个模块最后一次释放内存的机会,如:
PHP_MSHUTDOWN_FUNCTION(session) { UNREGISTER_INI_ENTRIES(); #ifdef HAVE_LIBMM PHP_MSHUTDOWN(ps_mm) (SHUTDOWN_FUNC_ARGS_PASSTHRU); #endif ps_serializers[PREDEFINED_SERIALIZERS].name = NULL; memset(&ps_modules[PREDEFINED_MODULES], 0, (MAX_MODULES-PREDEFINED_MODULES)*sizeof(ps_module *)); return SUCCESS; }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide
