search
HomeBackend DevelopmentPHP TutorialDetailed explanation of PHP underlying principles examples

Detailed explanation of PHP underlying principles examples

Mar 14, 2018 pm 02:35 PM
phpExampleDetailed explanation

I have used LAMP or LNMP for a long time, so how do lamps work or how are they connected? I usually just write programs and rarely think about their working principles: This article mainly shares with you the bottom layer of PHP Detailed explanation of the principle and examples, I hope it can help everyone.

The underlying working principle of PHP

Detailed explanation of PHP underlying principles examples
Figure 1 PHP structure

As can be seen from the figure, PHP is a 4-layer system from bottom to top

①Zend Engine

Zend is implemented entirely in pure C and is the core part of PHP. It translates PHP code (lexical, syntax analysis and other compilation processes) into executable opcode processing and implementation Corresponding processing methods, implementing basic data structures (such as hashtable, oo), memory allocation and management, and providing corresponding api methods for external calls are the core of everything. All peripheral functions are implemented around zend.

②Extensions

Around the zend engine, extensions provide various basic services in a component-based manner. Our common various built-in functions (such as array series), standard libraries, etc. are all through extensions To achieve this, users can also implement their own extensions as needed to achieve function expansion, performance optimization and other purposes (for example, the PHP middle layer and rich text parsing currently used by Tieba are typical applications of extensions).

③Sapi

The full name of Sapi is Server Application Programming Interface, which is the server application programming interface. Sapi allows PHP to interact with peripheral data through a series of hook functions. This is PHP's very elegant and A successful design successfully decouples and isolates PHP itself from upper-layer applications through SAPI. PHP can no longer consider how to be compatible with different applications, and the application itself can also implement different processing methods according to its own characteristics. We will introduce it later in the sapi chapter

④Upper-layer application

This is the PHP program we usually write. We can obtain various application modes through different sapi methods, such as implementing web through webserver Apply, run as a script from the command line, etc.

Architectural idea:

The engine (Zend) + component (ext) model reduces internal coupling

The middle layer (sapi) isolates the web server and PHP


If php is a car, then

The framework of the car is php itself

Zend is the engine (engine) of the car

The various components below Ext It is the wheel of a car

Sapi can be regarded as a road, and cars can run on different types of roads

And the execution of a php program means that the car runs on the road.

Therefore, we need: excellent performance engine + suitable wheels + correct runway

The relationship between Apache and php

Apache parses php through many Modules This is done using the php Module in it.

Detailed explanation of PHP underlying principles examples
To finally integrate php into the Apache system, you need to make some necessary settings for Apache. Here, we will take the mod_php5 SAPI operating mode of php as an example to explain. As for the concept of SAPI, we will explain it in detail later.

Assume that the versions we install are Apache2 and Php5, then you need to edit Apache’s main configuration file http.conf and add the following lines to it:

In Unix/Linux environment:

LoadModule php5_module modules/mod_php5.so

AddType application/x-httpd-php .php

Note: modules/mod_php5.so is mod_php5.so in the X system environment The installation location of the file.

In Windows environment:

LoadModule php5_module d:/php/php5apache2.dll

AddType application/x-httpd-php .php

Note: Among them, d:/php/php5apache2.dll is the installation location of the php5apache2.dll file in the Windows environment.

These two configurations tell Apache Server that any URL user request received in the future with php as the suffix needs to be processed by calling the php5_module module (mod_php5.so/ php5apache2.dll).

Apache’s life cycle

Detailed explanation of PHP underlying principles examples
Apache’s request processing process
Detailed explanation of PHP underlying principles examples

Apache request processing cycle detailed explanation

What are done in the 11 stages of the Apache request processing cycle?

1. Post-Read-Request phase

In the normal request processing process, this is the first stage where the module can insert hooks a stage. This stage can be exploited by modules that want to get into processing requests very early.

2. URI Translation Phase

Apache’s main job in this phase is to map the requested URL to the local file system. Modules can insert hooks at this stage to execute their own mapping logic. mod_alias uses this phase to work.

3. Header Parsing phase

Apache’s main job in this phase is to check the header of the request. Since the module can perform the task of checking request headers at any point in the request processing flow, this hook is rarely used. mod_setenvif uses this phase to work.

4. Access Control stage

Apache’s main job at this stage: Check whether access to the requested resource is allowed according to the configuration file. Apache's standard logic implements allow and deny directives. mod_authz_host uses this phase to work.

5. Authentication stage

The main work of Apache in this stage is to authenticate users according to the policy set in the configuration file and set the user name area. Modules can insert hooks at this stage to implement an authentication method.

6. Authorization phase

The main work of Apache in this phase is to check whether authenticated users are allowed to perform the requested operation according to the configuration file. The module can insert hooks at this stage to implement a user rights management method.

7. MIME Type Checking Phase

The main work of Apache in this phase is to determine the content processing function to be used based on the relevant rules of the MIME type of the requested resource. The standard modules mod_negotiation and mod_mime implement this hook.

8. FixUp stage

This is a general stage that allows the module to run any necessary processing before the content generator. Similar to Post_Read_Request, this is a hook that can capture any information and is also the most commonly used hook.

9. Response phase

The main work of Apache in this phase is to generate content returned to the client and be responsible for sending an appropriate reply to the client. This stage is the core part of the entire process.

10. Logging phase

The main work of Apache in this phase: recording transactions after the reply has been sent to the client. Modules may modify or replace Apache's standard logging.

11. CleanUp Phase

The main work of Apache in this phase: clean up the environment left after the completion of this request transaction, such as the processing of files and directories or the closing of Socket, etc. This It is the last stage of a request processing by Apache.

LAMP architecture:

Detailed explanation of PHP underlying principles examples
Four layers from bottom to top:

①liunx belongs to the bottom layer of the operating system

②apache server, belongs to Secondary server, communicates with Linux and PHP mysql association

Android system architecture diagram

Compare the architecture diagram of lamp and Android. It seems to be somewhat similar to the lamp architecture. I don’t understand Android, but it feels a bit similar. Experts can point out the difference. Thank you very much

From top to bottom:

Android architecture——————Explanation————LAMP architectureDetailed explanation of PHP underlying principles examples
1. Application Program——Specific application——web application

2. Application framework——java————PHP language and library

3. System runtime library:——Virtual machine ——WEB Server

⒋Linux Kernel: —Operating System——-L

in the lamp architecture has been using LAMP or LNMP for a long time, so how does the lamp work, or How are they connected? I usually just write programs and rarely think about the working principles between them:

PHP underlying working principle

Figure 1 PHP structure

As can be seen from the picture, PHP is a 4-layer system from bottom to topDetailed explanation of PHP underlying principles examples
①Zend engine

Zend is implemented in pure C and is the core part of PHP. It will Code translation (a series of compilation processes such as lexical and syntax analysis) processes executable opcodes and implements corresponding processing methods, implements basic data structures (such as hashtable, oo), memory allocation and management, and provides corresponding API methods. For external calls, it is the core of everything, and all peripheral functions are implemented around zend.

②Extensions

Around the zend engine, extensions provide various basic services in a component-based manner. Our common various built-in functions (such as array series), standard libraries, etc. are all through extensions To achieve this, users can also implement their own extensions as needed to achieve function expansion, performance optimization and other purposes (for example, the PHP middle layer and rich text parsing currently used by Tieba are typical applications of extensions).

③Sapi

The full name of Sapi is Server Application Programming Interface, which is the server application programming interface. Sapi allows PHP to interact with peripheral data through a series of hook functions. This is PHP's very elegant and A successful design successfully decouples and isolates PHP itself from upper-layer applications through SAPI. PHP can no longer consider how to be compatible with different applications, and the application itself can also implement different processing methods according to its own characteristics. We will introduce it later in the sapi chapter

④Upper-layer application

This is the PHP program we usually write. We can obtain various application modes through different sapi methods, such as implementing web through webserver Apply, run as a script from the command line, etc.

Architectural idea:

The engine (Zend) + component (ext) model reduces internal coupling

The middle layer (sapi) isolates the web server and PHP

If php is a car, then

The framework of the car is php itself

Zend is the engine of the car

The various components below Ext are the wheels of the car

Sapi can be seen as Highways, cars can run on different types of roads

And the execution of a php program means the car runs on the road.

Therefore, we need: excellent performance engine + suitable wheels + correct runway

The relationship between Apache and php

Apache parses php through many Modules This is done using the php Module in it.

Detailed explanation of PHP underlying principles examples
To finally integrate php into the Apache system, you need to make some necessary settings for Apache. Here, we will take the mod_php5 SAPI operating mode of php as an example to explain. As for the concept of SAPI, we will explain it in detail later.

Assume that the versions we install are Apache2 and Php5, then you need to edit Apache’s main configuration file http.conf and add the following lines to it:

In Unix/Linux environment:

LoadModule php5_module modules/mod_php5.so

AddType application/x-httpd-php .php

Note: modules/mod_php5.so is mod_php5.so in the X system environment The installation location of the file.

In Windows environment:

LoadModule php5_module d:/php/php5apache2.dll

AddType application/x-httpd-php .php

Note: Among them, d:/php/php5apache2.dll is the installation location of the php5apache2.dll file in the Windows environment.

These two configurations tell Apache Server that any Url user requests received in the future, with php as the suffix, need to call the php5_module module (mod_php5.so/php5apache2.dll) for processing.

Apache’s life cycle

Detailed explanation of PHP underlying principles examples
Apache’s request processing process
Detailed explanation of PHP underlying principles examples

Apache request processing cycle detailed explanation

What are done in the 11 stages of the Apache request processing cycle?

1. Post-Read-Request phase

In the normal request processing process, this is the first stage where the module can insert hooks a stage. This stage can be exploited by modules that want to get into processing requests very early.

2. URI Translation Phase

Apache’s main job in this phase is to map the requested URL to the local file system. Modules can insert hooks at this stage to execute their own mapping logic. mod_alias uses this phase to work.

3. Header Parsing phase

Apache’s main job in this phase is to check the header of the request. Since the module can perform the task of checking request headers at any point in the request processing flow, this hook is rarely used. mod_setenvif uses this phase to work.

4. Access Control Phase

The main work of Apache in this phase is to check whether access to the requested resources is allowed according to the configuration file. Apache's standard logic implements allow and deny directives. mod_authz_host uses this phase to work.

5. Authentication stage

The main work of Apache in this stage is to authenticate users according to the policy set in the configuration file and set the user name area. Modules can insert hooks at this stage to implement an authentication method.

6. Authorization phase

The main work of Apache in this phase is to check whether authenticated users are allowed to perform the requested operation according to the configuration file. The module can insert hooks at this stage to implement a user rights management method.

7. MIME Type Checking Phase

The main work of Apache in this phase is to determine the content processing function to be used based on the relevant rules of the MIME type of the requested resource. The standard modules mod_negotiation and mod_mime implement this hook.

8. FixUp stage

This is a general stage that allows the module to run any necessary processing before the content generator. Similar to Post_Read_Request, this is a hook that can capture any information and is also the most commonly used hook.

9. Response phase

The main work of Apache in this phase is to generate content returned to the client and be responsible for sending an appropriate reply to the client. This stage is the core part of the entire process.

10. Logging phase

The main work of Apache in this phase: recording transactions after the reply has been sent to the client. Modules may modify or replace Apache's standard logging.

11. CleanUp Phase

The main work of Apache in this phase: clean up the environment left after the completion of this request transaction, such as the processing of files and directories or the closing of Socket, etc. This It is the last stage of a request processing by Apache.

LAMP architecture:

Detailed explanation of PHP underlying principles examples
Four layers from bottom to top:

①liunx belongs to the bottom layer of the operating system

②apache server, belongs to Secondary server, communicates between Linux and PHP

③php: It is a server-side programming language and is associated with apache through the php_module module

④mysql and other web services: belong to application services, and are associated with mysql through PHP's Extensions plug-in module

Android system architecture diagram

Compare lamp with Android's architecture diagram, it seems to be similar to lamp The architecture is somewhat similar. I don’t understand Android, but it feels a bit similar. Experts can point out the differences. I would be very grateful.

Detailed explanation of PHP underlying principles examples
From top to bottom:

Android architecture ——————Description——–LAMP architecture

1. Application——–Specific application——–Web application

2. Application framework——java————- PHP language and library

3. System runtime library: --Virtual machine --WEB server

⒋Linux kernel: --Operating system ---L

## in lamp architecture #Related recommendations:

Explanation of the underlying operating mechanism and principles of PHP

#PHP underlying analysis: About copy-on-write cow cow plural win color forum ww7349cow Japan cow Cow Milk Stone

In-depth understanding of the underlying mechanism of PHP_PHP Tutorial

The above is the detailed content of Detailed explanation of PHP underlying principles examples. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
How can you prevent session fixation attacks?How can you prevent session fixation attacks?Apr 28, 2025 am 12:25 AM

Effective methods to prevent session fixed attacks include: 1. Regenerate the session ID after the user logs in; 2. Use a secure session ID generation algorithm; 3. Implement the session timeout mechanism; 4. Encrypt session data using HTTPS. These measures can ensure that the application is indestructible when facing session fixed attacks.

How do you implement sessionless authentication?How do you implement sessionless authentication?Apr 28, 2025 am 12:24 AM

Implementing session-free authentication can be achieved by using JSONWebTokens (JWT), a token-based authentication system where all necessary information is stored in the token without server-side session storage. 1) Use JWT to generate and verify tokens, 2) Ensure that HTTPS is used to prevent tokens from being intercepted, 3) Securely store tokens on the client side, 4) Verify tokens on the server side to prevent tampering, 5) Implement token revocation mechanisms, such as using short-term access tokens and long-term refresh tokens.

What are some common security risks associated with PHP sessions?What are some common security risks associated with PHP sessions?Apr 28, 2025 am 12:24 AM

The security risks of PHP sessions mainly include session hijacking, session fixation, session prediction and session poisoning. 1. Session hijacking can be prevented by using HTTPS and protecting cookies. 2. Session fixation can be avoided by regenerating the session ID before the user logs in. 3. Session prediction needs to ensure the randomness and unpredictability of session IDs. 4. Session poisoning can be prevented by verifying and filtering session data.

How do you destroy a PHP session?How do you destroy a PHP session?Apr 28, 2025 am 12:16 AM

To destroy a PHP session, you need to start the session first, then clear the data and destroy the session file. 1. Use session_start() to start the session. 2. Use session_unset() to clear the session data. 3. Finally, use session_destroy() to destroy the session file to ensure data security and resource release.

How can you change the default session save path in PHP?How can you change the default session save path in PHP?Apr 28, 2025 am 12:12 AM

How to change the default session saving path of PHP? It can be achieved through the following steps: use session_save_path('/var/www/sessions');session_start(); in PHP scripts to set the session saving path. Set session.save_path="/var/www/sessions" in the php.ini file to change the session saving path globally. Use Memcached or Redis to store session data, such as ini_set('session.save_handler','memcached'); ini_set(

How do you modify data stored in a PHP session?How do you modify data stored in a PHP session?Apr 27, 2025 am 12:23 AM

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Give an example of storing an array in a PHP session.Give an example of storing an array in a PHP session.Apr 27, 2025 am 12:20 AM

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

How does garbage collection work for PHP sessions?How does garbage collection work for PHP sessions?Apr 27, 2025 am 12:19 AM

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

See all articles

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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.