Procedures for executing PHP programs

Source: Internet
Author: User

The Web environment we assume as Apache. When compiling PHP, in order to enable Apache to support PHP, we will generate a mod_php5.so module. Apache loads this module, which is transferred to the Mod_php5.so module when the URL accesses the. php file. This is what we often call the SAPI. The English name is: Server application programming Interface. SAPI is actually a general term, under which there are isapi,cli sapi,cgi and so on. With it, it is easy to interact with other things, such as apache,iis,cgi.

Apache will be launched after the mod_pho5.so module hook handler registered in, when Apache detects that the URL is a php file, this time will give control to SAPI. After entering the SAPI, the Php_init_handler function of the sapi/apache/mod_php5.c file is executed first, and a snippet of code is extracted here:

static void php_init_handler(server_rec *s, pool *p) {      register_cleanup(p, NULL, (void (*)(void *))apache_php_module_shutdown_wrapper, (void (*)(void *))php_module_shutdown_for_exec);      if (!apache_php_initialized) {          apache_php_initialized = 1;          #ifdef ZTS          tsrm_startup(1, 1, 0, NULL);          # endif          sapi_startup(&apache_sapi_module);          php_apache_startup(&apache_sapi_module);      }      # if MODULE_MAGIC_NUMBER >= 19980527      {          TSRMLS_FETCH();          if (PG(expose_php)) {              ap_add_version_component( "PHP/" PHP_VERSION);          }      }      # endif }

The function mainly calls two functions: Sapi_startup (&apache_sapi_module); Php_apache_startup (&apache_sapi_module);

SAPI_API void sapi_startup(sapi_module_struct *sf) {      sf->ini_entries = NULL;      sapi_module = *sf;      .................      sapi_globals_ctor(&sapi_globals);      ................     virtual_cwd_startup(); /* Could use shutdown to free the main cwd but it would just slow it down for CGI */      ..................     reentrancy_startup(); }

Sapi_startup Create a sapi_globals_struct struct. Sapi_globals_struct holds basic information about Apache requests, such as server information, headers, encodings, and so on. Sapi_startup executes php_apache_startup after execution is complete.

static int php_apache_startup(sapi_module_struct *sapi_module) {      if (php_module_startup(sapi_module, &apache_module_entry, 1) == FAILURE) {          return FAILURE;      } else {          return SUCCESS;      } }

Php_module_startup content is too much, here is about the general role:

1. Initialize the zend_utility_functions structure. This structure is a function pointer that sets Zend, such as error handling function, output function, stream operation function, etc.

2. Set environment variables.

3. Load the php.ini configuration.

4. Load PHP built-in extensions.

5. Write the log.

6. Register the PHP internal set of functions.

7. Call Php_ini_register_extensions, load all external extensions

8. Turn on all extensions

9. Some cleanup operations.

Focus on 3,4,7,8.

load php.ini Configuration

if (php_init_config(TSRMLS_C) == FAILURE) {      return FAILURE; }

The Php_init_config function checks all the php.ini configurations here, and finds all the loaded modules and adds them to the php_extension_lists structure.

load PHP Built-in extensions

Call Zend_register_standard_ini_entries to load all of PHP's built-in extensions, such as Array,mysql.

call php_ini_register_extensions, load all external extensions

Main/php_ini.c

void php_ini_register_extensions(TSRMLS_D) {      zend_llist_apply(&extension_lists.engine, php_load_zend_extension_cb TSRMLS_CC);      zend_llist_apply(&extension_lists.functions, php_load_php_extension_cb TSRMLS_CC);     zend_llist_destroy(&extension_lists.engine);      zend_llist_destroy(&extension_lists.functions); }

zend_llist_apply function Traversal extension_lists execution will drop function PHP_LOAD_PHP_EXTENSION_CB

Php_load_php_extension_cb

static void php_load_zend_extension_cb(void *arg TSRMLS_DC) {      zend_load_extension(*((char **) arg)); }

The function finally calls the

if ((module_entry = zend_register_module_ex(module_entry TSRMLS_CC)) == NULL) {      DL_UNLOAD(handle);      return FAILURE; }

Put the extended information into the hash table module_registry, ZEND/ZEND_API.C

if (zend_hash_add(&module_registry, lcname, name_len+1, (void *)module, sizeof(zend_module_entry), (void**)&module_ptr)==FAILURE) {      zend_error(E_CORE_WARNING, "Module \‘%s\‘ already loaded" , module->name);      efree(lcname);      return NULL; }

Finally, Zend_startup_modules (Tsrmls_c); Sorts the modules and detects if they are registered with the Module_registry hash table. Zend_startup_extensions (); Execute extension->startup (extension); start the extension.

Procedures for executing PHP programs

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.