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'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.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

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.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

See all articles