PHP kernel-start and end of user requests

Source: Internet
Author: User
Tags apache php sapi
PHP kernel-anyone who has installed apache at the beginning and end of user requests knows that after installing PHP, we only configure apache, mainly to add the mod_php5.so extension, after apache is restarted, PHP can be used normally. during this process, we have never manually started PHP-related processes. in this case, PHP kernel-start and end of user requests

Anyone who has installed apache knows that after installing PHP, we only configure apache. we mainly add the mod_php5.so extension and restart apache to use PHP normally, in this process, we have never started PHP-related processes manually. how does it start?

It is started with apache startup. The PHP program installed on the server communicates with apache through the mod_php5.so module. In fact, in my previous blog, we know that, this module is essentially SAPI. In this blog post, I will discuss a user request process, mainly how communication occurs.


PHP consists of three modules: Kernel, Zend Engine, and extension.

The kernel is mainly used to process requests, file streams, error processing, and other related operations;

The Zend Engine (ZE) is used to convert the source file to the machine language, and then run it on the virtual machine.

The extension layer is a set of functions, class libraries, and streams. PHP uses the extension layer to perform some specific operations.


When a request arrives, PHP starts up in two phases:

  • The first phase is started with apache startup, that is, starting the PHP module. this simple process is discussed in blog "Apache PHP module startup. This stage generates some environment variables that are visible throughout the SAPI lifecycle.
  • The second stage is to generate some environment and variables related to the request after a request arrives.
In the second stage, when a page request arrives, the SAPI layer (APACHE) gives control to the PHP layer. in this case, PHP sets the environment variables used to process this request. PHP then calls the RINT method of each module to initialize the request. The premise is that these modules are configured in the php. ini file. For example, request initialization of the mysql module:
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;}
Another example is session module request initialization.
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;}
This initialization initializes the $ _ SESSION variable.
When PHP is disabled, there are two phases,
  • Phase 1: at the end of a page, the PHP_RSHUTDOWN_FUNCTION method of each module is called in sequence to clear the generated variables and symbols. For example
PHP_RSHUTDOWN_FUNCTION(session){        php_session_flush(TSRMLS_C);        php_rshutdown_session_globals(TSRMLS_C);        return SUCCESS;}
  • Phase 2: Finally, all requests have been processed, and the SAPI is also ready to be closed. PHP starts to execute Step 2: PHP calls the MSHUTDOWN method of each extension, this is the last chance for each module to release memory, for example:
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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.