SlideShare a Scribd company logo
The PHP Lifecycle
Yahoo! . 惠新宸
       Tel : 86111
       Mail: laruence@yahoo.com.cn
Summary

•   B/S(HTTP Request Cycle)
•   Apache Life Cycle
•   Apache Parse Request Cycle
•   PHP Life Cycle
•   Cooperating with Apache
•   PHP Parse/Execute Process
•   Now We Can…


         Linux/Unix + Apache 1.3x + PHP 5.x
B/S




1. Client Request
2. Server Parse Requst and Send Response
3. Client interpret Responded Text/HTML/Json etc…
Apache Life Cycle
Apache Parse Request Cycle
PHP Life Cycle

•   In command line mode(CLI)
PHP Life Cycle con’t

•   PHP_MODULE_ENTRY(php_mysql.c 215 行 )
PHP Life Cycle con’t

•   PHP_MINIT_FUNCTION(php_mysql.c 378 行 )
PHP Life Cycle con’t

•   PHP_RINIT_FUNCTION(php_mysql.c 473 行 )
PHP Life Cycle con’t

•   PHP_SHUTDOWNS(php_mysql.c 403 行 )
PHP Life Cycle con’t

•   In DSO mode
     – Single process
PHP Life Cycle con’t

•   In DSO mode
     – Multiprocess
PHP Life Cycle con’t

•   In CGI mode
     – Multithreaded
Cooperation

•   Apache 1.3x + PHP 5.x in DSO mode
•   Dynamic Shared Objects (DSO)
    – mod_dso
        • src/modules/standard/mod_so.c 347 行
Cooperation con’t

•   Php Module 注册 Handler
    – mod_php5.c 963 行
Cooperation con’t

•   Php Module 注册 Handler
    – mod_php5.c 987 行
Cooperation con’t

•   http.conf
PHP Parse/Execute Process
PHP Parse/Execute Process con’t

•   Scan(lex)
     –   Zend/zend_language_scanner.c 3047 行

    ZEND_API zend_op_array *compile_file(zend_file_handle *file_handle, int type TSRMLS_DC){
             retval = op_array;
             ….
             return retval;}

     –   Zend/zend_language_scanner.l
                                                       T_OPEN_TAG: '<?php '
                                                       =
          <?php                                        T_LNUMBER: '1'
            $sum = 1 + 2;                              +
            echo “1+2=“.$sum;                          T_LNUMBER: '2'
          ?>                                           T_ECHO: 'echo''
                                                       T_CONSTANT_ENCAPSED_STRI
                                                       NG: ''1+2=''.
                                                       T_CLOSE_TAG: '?>'
PHP Parse/Execute Process con’t

•   Parse/Complie(yacc)
     – zend_language_parser.y

     T_OPEN_TAG: '<?php '
     =                          Opcode     Op1     Op2        Result
     T_LNUMBER: '1'             ADD         1       2          $tmp0
     +                          ASSIGN   $cv0(sum) $tmp0      $var1
     T_LNUMBER: '2'             CONCAT    '1+2='  $cv0(sum)   $tmp2
     T_ECHO: 'echo''            ECHO      $tmp2
     T_CONSTANT_ENCAPSED_STRI   RETURN      1
     NG: ''1+2=''.
     T_CLOSE_TAG: '?>'
PHP Parse/Execute Process con’t

•   Execute
    – Zend/zend_execute.c

     void (*zend_execute) (zend_op_array *op_array TSRMLS_DC);



•   Zend_op_array
    – Zend/zend_compile.h
                                 struct zend_op{
                                  opcode_handler_t handler;
                                  znoderesult;
                                  znodeop1;
                                  znodeop2;
                                  ulongextended_value;
                                  uintlineno;
                                  zend_ucharopcode;
                                 };
PHP Parse/Execute Process con’t

•   Execute
    – Zend/zend_opcode.c 428 行
A sample

•   Clien Request
A sample

•    http_main.c 8053
      –    main() call ap_main

•         ap_main => REALMAIN
      –    REALMAIN(argc, argv)
      –    http_main.c

•    child_main
      –    Call child_sub_main 6134

•    chile_sub_main
      –    loop worker
      –    call ap_process_request(r) on request

•    http_reqeust.c 1308
•    ap_process_request
      –    call process_request_internal

•    process_request_internal
      –    call ap_invoke_handler

Application/x-httpd-php -> send_parsed_php
A sample

•   Server Pares
     – URI
        • Filename -> /test/index.php
    – User Auth
    – …
    – MIME type Check
        • application/x-httpd-http
    – Call handler
        • send_parsed_php(request_rec *)
        • Zend_execute_script(…..request_rec->filename);
A sample

• Zend/zend.c 1073 行
A sample


•   Index.php

        <?php
           echo 2+3;
        ?>
A sample

•   Zend_language_scanner.l 362 行




•   Zend_language_parse.c 62 行
A sample

•    Parser
      –   Zend_language_parse.y




      –   Zend_compile.c 258 行
A sample

•   Opcodes
A sample

•   Zend_execute       zend.c 594 行
     –   Zend_execute => execute
•   execute zend_vm_execute.h 34 行

         ZEND_API void execute(zend_op_array *op_array TSRMLS_DC)


•   Generate Output
•   Return to Apache Server process
•   Server send Responds Text to Client.
Now We Can….

•   1, 服务器的 Access log 没有任何输出
•   2, 隐藏 PHP 文件类型
•   3, PHP 出错的阶段
•   4, 编写 PHP 扩展
•   5, …
Documents

•   《 Apache Server 源代码分析》
•   《 Writing Apache Modules with Perl and C 》
•   《 Extending and Embedding PHP 》
•   《 PHP 手册》:“ Zend API :深入 PHP 内核”
•    http://blog.csdn.net/laruence
谢谢大家!

More Related Content

PDF
PhD Defense
PPTX
Json Web Token - JWT
PDF
JSON Web Token
PPTX
Bresenham's line drawing algorithm
PDF
Deep learning based object detection basics
PDF
PyTorch Python Tutorial | Deep Learning Using PyTorch | Image Classifier Usin...
PPTX
PhD Defense
Json Web Token - JWT
JSON Web Token
Bresenham's line drawing algorithm
Deep learning based object detection basics
PyTorch Python Tutorial | Deep Learning Using PyTorch | Image Classifier Usin...

What's hot (20)

PDF
cours j2ee -présentation
PPTX
HTTP request and response
PPT
4 internet programming
PDF
PHP Basic & Variables
PPTX
Servlets
PPTX
Knowledge based agents
PPTX
Unification and Lifting
PPTX
Aspect Oriented Programing - Introduction
PDF
jQuery for beginners
PPTX
Java vs python
PPTX
Game Project / Working with Unity
PPTX
First order logic
PPTX
Basic Concept of Node.js & NPM
PPTX
Introduction to PyTorch
PPTX
Forward and Backward chaining in AI
PPT
Lecture 9: Dynamic web application
PPTX
Computer graphics chapter 4
PPTX
Object detection with deep learning
PPT
opengl.ppt
cours j2ee -présentation
HTTP request and response
4 internet programming
PHP Basic & Variables
Servlets
Knowledge based agents
Unification and Lifting
Aspect Oriented Programing - Introduction
jQuery for beginners
Java vs python
Game Project / Working with Unity
First order logic
Basic Concept of Node.js & NPM
Introduction to PyTorch
Forward and Backward chaining in AI
Lecture 9: Dynamic web application
Computer graphics chapter 4
Object detection with deep learning
opengl.ppt
Ad

Similar to The Php Life Cycle (20)

PDF
Phpをいじり倒す10の方法
PDF
Cli the other sapi pbc11
PDF
CLI, the other SAPI phpnw11
PDF
Cli the other SAPI confoo11
KEY
Anatomy of a PHP Request ( UTOSC 2010 )
PDF
Quick tour of PHP from inside
PDF
Php engine
PDF
PHP Internals and Virtual Machine
ODP
Php opcodes sep2008
PDF
Create your own PHP extension, step by step - phpDay 2012 Verona
PDF
Debugging: Rules And Tools - PHPTek 11 Version
PDF
[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이
PDF
PHP & Performance
KEY
DPC 2012 : PHP in the Dark Workshop
PDF
Symfony 2 (PHP Quebec 2009)
PDF
Performance tuning with zend framework
PDF
Debugging: Rules & Tools
PDF
"Развитие ветки PHP-7"
PDF
Tips
PDF
Phpをいじり倒す10の方法
Cli the other sapi pbc11
CLI, the other SAPI phpnw11
Cli the other SAPI confoo11
Anatomy of a PHP Request ( UTOSC 2010 )
Quick tour of PHP from inside
Php engine
PHP Internals and Virtual Machine
Php opcodes sep2008
Create your own PHP extension, step by step - phpDay 2012 Verona
Debugging: Rules And Tools - PHPTek 11 Version
[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이
PHP & Performance
DPC 2012 : PHP in the Dark Workshop
Symfony 2 (PHP Quebec 2009)
Performance tuning with zend framework
Debugging: Rules & Tools
"Развитие ветки PHP-7"
Tips
Ad

More from Xinchen Hui (12)

PPTX
A brief to PHP 7.3
PDF
PHP7.1 New Features & Performance
PDF
The secret of PHP7's Performance
PDF
PHP7 - For Its Best Performance
PDF
A History of PHP
PDF
High Performance Solution for PHP7
PDF
PHP7 - The New Engine for old good train
PDF
微博Lamp性能优化之路(2014)
PDF
Weibo lamp improvements
PDF
Php 5.4 performance
PPT
China PHP Technology Summit 2011 ppt
PPT
Php performance
A brief to PHP 7.3
PHP7.1 New Features & Performance
The secret of PHP7's Performance
PHP7 - For Its Best Performance
A History of PHP
High Performance Solution for PHP7
PHP7 - The New Engine for old good train
微博Lamp性能优化之路(2014)
Weibo lamp improvements
Php 5.4 performance
China PHP Technology Summit 2011 ppt
Php performance

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Spectroscopy.pptx food analysis technology
PPT
Teaching material agriculture food technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
cuic standard and advanced reporting.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Chapter 3 Spatial Domain Image Processing.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Spectroscopy.pptx food analysis technology
Teaching material agriculture food technology
“AI and Expert System Decision Support & Business Intelligence Systems”
Diabetes mellitus diagnosis method based random forest with bat algorithm
Spectral efficient network and resource selection model in 5G networks
20250228 LYD VKU AI Blended-Learning.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
cuic standard and advanced reporting.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Network Security Unit 5.pdf for BCA BBA.
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
sap open course for s4hana steps from ECC to s4
Dropbox Q2 2025 Financial Results & Investor Presentation
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

The Php Life Cycle

  • 1. The PHP Lifecycle Yahoo! . 惠新宸 Tel : 86111 Mail: laruence@yahoo.com.cn
  • 2. Summary • B/S(HTTP Request Cycle) • Apache Life Cycle • Apache Parse Request Cycle • PHP Life Cycle • Cooperating with Apache • PHP Parse/Execute Process • Now We Can… Linux/Unix + Apache 1.3x + PHP 5.x
  • 3. B/S 1. Client Request 2. Server Parse Requst and Send Response 3. Client interpret Responded Text/HTML/Json etc…
  • 6. PHP Life Cycle • In command line mode(CLI)
  • 7. PHP Life Cycle con’t • PHP_MODULE_ENTRY(php_mysql.c 215 行 )
  • 8. PHP Life Cycle con’t • PHP_MINIT_FUNCTION(php_mysql.c 378 行 )
  • 9. PHP Life Cycle con’t • PHP_RINIT_FUNCTION(php_mysql.c 473 行 )
  • 10. PHP Life Cycle con’t • PHP_SHUTDOWNS(php_mysql.c 403 行 )
  • 11. PHP Life Cycle con’t • In DSO mode – Single process
  • 12. PHP Life Cycle con’t • In DSO mode – Multiprocess
  • 13. PHP Life Cycle con’t • In CGI mode – Multithreaded
  • 14. Cooperation • Apache 1.3x + PHP 5.x in DSO mode • Dynamic Shared Objects (DSO) – mod_dso • src/modules/standard/mod_so.c 347 行
  • 15. Cooperation con’t • Php Module 注册 Handler – mod_php5.c 963 行
  • 16. Cooperation con’t • Php Module 注册 Handler – mod_php5.c 987 行
  • 19. PHP Parse/Execute Process con’t • Scan(lex) – Zend/zend_language_scanner.c 3047 行 ZEND_API zend_op_array *compile_file(zend_file_handle *file_handle, int type TSRMLS_DC){ retval = op_array; …. return retval;} – Zend/zend_language_scanner.l T_OPEN_TAG: '<?php ' = <?php T_LNUMBER: '1' $sum = 1 + 2; + echo “1+2=“.$sum; T_LNUMBER: '2' ?> T_ECHO: 'echo'' T_CONSTANT_ENCAPSED_STRI NG: ''1+2=''. T_CLOSE_TAG: '?>'
  • 20. PHP Parse/Execute Process con’t • Parse/Complie(yacc) – zend_language_parser.y T_OPEN_TAG: '<?php ' = Opcode Op1 Op2 Result T_LNUMBER: '1' ADD 1 2 $tmp0 + ASSIGN $cv0(sum) $tmp0 $var1 T_LNUMBER: '2' CONCAT '1+2=' $cv0(sum) $tmp2 T_ECHO: 'echo'' ECHO $tmp2 T_CONSTANT_ENCAPSED_STRI RETURN 1 NG: ''1+2=''. T_CLOSE_TAG: '?>'
  • 21. PHP Parse/Execute Process con’t • Execute – Zend/zend_execute.c void (*zend_execute) (zend_op_array *op_array TSRMLS_DC); • Zend_op_array – Zend/zend_compile.h struct zend_op{ opcode_handler_t handler; znoderesult; znodeop1; znodeop2; ulongextended_value; uintlineno; zend_ucharopcode; };
  • 22. PHP Parse/Execute Process con’t • Execute – Zend/zend_opcode.c 428 行
  • 23. A sample • Clien Request
  • 24. A sample • http_main.c 8053 – main() call ap_main • ap_main => REALMAIN – REALMAIN(argc, argv) – http_main.c • child_main – Call child_sub_main 6134 • chile_sub_main – loop worker – call ap_process_request(r) on request • http_reqeust.c 1308 • ap_process_request – call process_request_internal • process_request_internal – call ap_invoke_handler Application/x-httpd-php -> send_parsed_php
  • 25. A sample • Server Pares – URI • Filename -> /test/index.php – User Auth – … – MIME type Check • application/x-httpd-http – Call handler • send_parsed_php(request_rec *) • Zend_execute_script(…..request_rec->filename);
  • 27. A sample • Index.php <?php echo 2+3; ?>
  • 28. A sample • Zend_language_scanner.l 362 行 • Zend_language_parse.c 62 行
  • 29. A sample • Parser – Zend_language_parse.y – Zend_compile.c 258 行
  • 30. A sample • Opcodes
  • 31. A sample • Zend_execute zend.c 594 行 – Zend_execute => execute • execute zend_vm_execute.h 34 行 ZEND_API void execute(zend_op_array *op_array TSRMLS_DC) • Generate Output • Return to Apache Server process • Server send Responds Text to Client.
  • 32. Now We Can…. • 1, 服务器的 Access log 没有任何输出 • 2, 隐藏 PHP 文件类型 • 3, PHP 出错的阶段 • 4, 编写 PHP 扩展 • 5, …
  • 33. Documents • 《 Apache Server 源代码分析》 • 《 Writing Apache Modules with Perl and C 》 • 《 Extending and Embedding PHP 》 • 《 PHP 手册》:“ Zend API :深入 PHP 内核” • http://blog.csdn.net/laruence