Although most PHP engineers do not need to know how PHP's C code core works, some people may know that there is a DL () function. Or the use of some third-party class libraries, which is one of the key points of this article.
Hopefully this article will help the engineers who want to bring PHP to a wider boundary.
Let's look at the running flow of a PHP request:
Browser User--->web server (apache,nginx)--->zend engine reads PHP code files from File system--->zend interpreter works
---> Execute interpreted Code-->zend engine registered function interfaces--built-in modules or external modules for each need extension--back-end resources such as database Memcache
which
The function interface registered by the Zend Engine is a variety of PHP functions that PHP engineers often contact.
The external module extension is the individual so files (Linux) or DLL files (windwos) compiled by PHP.
The content of the code browser that executes the explanation is returned from here.
The built-in module, which is PHP, carries the boot module each time it starts.
From the flowchart above you can see that PHP can be extended from 3 points. 1 external Module Extension 2 Zend Engine 3 built-in modules, I'll discuss them here.
External module extension.
If you have used DL () you have been exposed to these external extensions. The external extension module file is placed on your hard drive and is loaded into memory when the PHP script is running, and is loaded only when needed.
When this script is finished, he will be freed by memory, which generally runs slowly but does not account for resources. You don't need to recompile a PHP.
Built-in Modules
Although it is also a module outside of the Zend engine, it is somewhat different from the external module extension, and he is already in PHP. He will make your compiled PHP volume larger, if there is a change, you must recompile PHP. The built-in modules make
PHP's memory gets bigger, but it can be called more quickly. In our tests, some modules run in built-in mode with a speed of more than 30%.
Zend Engine
First of all, I definitely do not recommend you to modify the Zend engine. Some features of the PHP language can only be implemented in the Zend engine. For example, if you want to change the name of the array keyword, you can do it here.
In the PHP source code you downloaded, the code that starts with Zend is the Zend engine.
The general PHP source directory structure is similar to the following:
Main source code of PHP,
Ext PHP Extensions
SAPI API interaction layer code with different servers
Zend Zend Engine Section
TSRM thread safety related module code
Here is an example of how PHP can be extended with a simple module:
First of all, PHP code has its own set of standards, you need to comply with, or it may cause your module can not release variables or other problems, these standards include macro definitions, variable declarations and so on. You can go to the official website for detailed instructions.
/* Extended Standard header */
#include "Php.h"
/* Declare this so exported function */
Zend_function (Helloworld_module);
/* Zend Engine Registered function interface */
Zend_function_entry helloworldmod_interfaces[] =
{
Zend_fe (Helloworld_module, NULL)
{null, NULL, NULL}
};
/* This is the declaration entity for this module, and its value is actually useful when the module is compiled.
Zend_module_entry Helloworldmod_module_entry =
{
Standard_module_header,
"Hello World",
Helloworldmod_interfaces,
Null
Null
Null
Null
Null
No_version_yet,
Standard_module_properties
};
/* Declare a record to the Zend engine to indicate that helloworldmod_module_entry belongs to the helloworldmod.so dynamic library */
#if Compile_dl_helloworld_module
Zend_get_module (Helloworldmod)
#endif
/* This is the real code for our new function */
Zend_function (Helloworld_module)
{
return "Hello,world";
}
We can modify the necessary compilation configuration information according to other extended config.m4 files. Here This module is almost an empty config.m4 file on the line,
Then use Phpize to generate the Configure file and then the./configure && make && do install to compile a copy of our dynamic library
test.php
Echo Helloworld_module ();
?>
Output:
"Hello,world"
The PHP extension has been completed, and we have been inside the C Code of PHP, but in some cases this is not enough. We need to go deep into C language Call C library in the process, under Linux a very strong tool is the LD_PRELOAD environment variable
The LD_PRELOAD environment variable is a filter where the compiler finds a function or global variable that is referenced in the program, such as calling a method to start a network connection in the C Code of PHP, in fact by dynamically linking
To find the Linux C library function Connect, these linked files are generally placed under LIB, which also provides a point of entry for our code execution that affects PHP. Because the PHP program will check the ld_preload before dynamically loading the function connect in Lib
There is no this connect function in the provided dynamic library, where we can interfere with PHP's behavior.
Here's an example of a simple filter network access that shows how to do this:
First a code that prepares the so file as the value of the LD_PRELOAD environment variable.
Lp_demo.c
#include
#include
#include
#include
#include
#include
Define our own Connect function
int connect (int sockfd, const struct SOCKADDR *serv_addr, socklen_t
Addrlen) {
static int (*CONNECT_LINUXC) (int, const struct sockaddr*, socklen_t) =null;
unsigned char *ip_char;
Use the Lsym rtld_next option to bypass the Connect method of the LD_PRELOAD environment variable to find the function of C library
if (!CONNECT_LINUXC) connect_linuxc=dlsym (Rtld_next, "Connect");
ip_char=serv_addr->sa_data;
ip_char+=2;
192.168.2.3 found it.
if ((*ip_char==192) && (* (ip_char+1) ==168) && (* (ip_char+2) ==2) && (* (ip_char+3) ==3)) {
Simply returns a code with a permission error
return eacces;
}
Call the Real Connect method
Return Connect_linuxc (Sockfd,serv_addr,addrlen);
}
Compiling into so files
$ gcc-o lp_demo.so-shared LP_DEMO.C-LDL
Test file test.php
File_get_contents ("http://192.168.2.3/");
?>
How to use
ld_preload=lp_demo.so PHP test.php
In this way he will not be able to access the 192.168.2.3 such as our internal URLs. Plays a very good role in sandbox.
In addition, we can use functions such as fwrite fopen to transfer PHP's read and write operations to the file system to back-end resources such as Mencache,nosql.
Finally, even if we have gone deep into the interior of the C library, it does not mean that we go to the bottom, under the C library, there are a bunch of sys_ functions, they are the real function in the kernel space, is not discussed here.
http://www.bkjia.com/PHPjc/477195.html www.bkjia.com true http://www.bkjia.com/PHPjc/477195.html techarticle Although most PHP engineers do not need to know how PHP's C code core works, some people may know that there is a DL () function. Or use some third-party class libraries, these are the ...