Home Backend Development PHP Tutorial PHP内核-用户请求的开始跟结束

PHP内核-用户请求的开始跟结束

Jun 13, 2016 pm 01:12 PM
apache php quot session

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;
}
Copy after login
再如session模块的请求初始化

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;
}
Copy after login
这个初始化会初始化$_SESSION变量。


在PHP关闭的时候,也分两个阶段,

  • 第一阶段: 在一个页面结束的时候,会按照顺序调用各个模块的PHP_RSHUTDOWN_FUNCTION方法,清除所产生的变量和符号。如

PHP_RSHUTDOWN_FUNCTION(session)
{
        php_session_flush(TSRMLS_C);
        php_rshutdown_session_globals(TSRMLS_C);

        return SUCCESS;
}
Copy after login
  • 第二阶段:最后,所有的请求都已处理完毕,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;
}
Copy after login


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

How to set the cgi directory in apache How to set the cgi directory in apache Apr 13, 2025 pm 01:18 PM

To set up a CGI directory in Apache, you need to perform the following steps: Create a CGI directory such as "cgi-bin", and grant Apache write permissions. Add the "ScriptAlias" directive block in the Apache configuration file to map the CGI directory to the "/cgi-bin" URL. Restart Apache.

How to start apache How to start apache Apr 13, 2025 pm 01:06 PM

The steps to start Apache are as follows: Install Apache (command: sudo apt-get install apache2 or download it from the official website) Start Apache (Linux: sudo systemctl start apache2; Windows: Right-click the "Apache2.4" service and select "Start") Check whether it has been started (Linux: sudo systemctl status apache2; Windows: Check the status of the "Apache2.4" service in the service manager) Enable boot automatically (optional, Linux: sudo systemctl

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

How to delete more than server names of apache How to delete more than server names of apache Apr 13, 2025 pm 01:09 PM

To delete an extra ServerName directive from Apache, you can take the following steps: Identify and delete the extra ServerName directive. Restart Apache to make the changes take effect. Check the configuration file to verify changes. Test the server to make sure the problem is resolved.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

How to view your apache version How to view your apache version Apr 13, 2025 pm 01:15 PM

There are 3 ways to view the version on the Apache server: via the command line (apachectl -v or apache2ctl -v), check the server status page (http://<server IP or domain name>/server-status), or view the Apache configuration file (ServerVersion: Apache/<version number>).

See all articles