Home >Backend Development >PHP Tutorial >Implementing PHP compilation and execution separation (separating compilation and execution_PHP tutorial
I just chatted with everyone in the PHP group, and agreed that everyone would write an article on how to implement PHP source code encryption. Taking advantage of this opportunity where QA is smoking, I will write down some ideas on this issue. I introduced in my previous article that when ZE (Zend engine) executes a PHP script, it will go through compilation->execution, but it will recompile the PHP file every time it is executed. There is no separation of compilation and execution. In the compilation and execution phase of ZE, there are two important functions: ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC); and ZEND_API void (*zend_execute)(zend_op_array *op_array TSRMLS_DC); zend_compile_file is responsible for compiling the script file to be executed into op codes composed of ZE’s basic instruction sequence, and then handing the op codes to zend_execute for execution to get the results of our script. So, we can completely separate the execution and compilation of PHP by modifying the default zend_complie_file and zend_execute. Furthermore, we can also implement the encryption and decryption of our scripts on this basis. We implement this function through a PHP extension module. First, we need to initialize the module: PHP_MINIT_FUNCTION(sample) In our my_compile_file, determine whether our file is a compiled file, assuming the suffix is *.ze. static zend_op_array *my_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC) op_array = old_compile_file (file_handle, type TSRMLS_CC); //Call the default compile and intercept the output In this way, we have achieved support for compiled files and file compilation. Then, we need to write our execution function: static void my_execute(zend_op_array *op_array TSRMLS_DC) Maybe you want to ask why we need to wrap the future execution function. Haha, I just want to explain that one way is to intercept this stuff. What's the use? It depends on readers what requirements you have can be realized through this method :). Writing this, you may understand that if you want to encrypt a file, then define an encrypted file type, such as *.zec, and then in my_compile_file, determine the file type. If it is an encrypted file, then execute Decryption, hey, is it easy? As for how to encrypt, you have to ask yourself, what method do you want to use, but remember, it must be reversible~~^_^.
{
old_compile_file = zend_compile_file; //Save the scene
old_execute = zend_execute;
zend_compile_file = my_compile_file; //Intercept
zend_execute = my_execute;
return SUCCESS;
}
{
if(strstr(file_handle->filename, ".ze") != NULL){//It is a compiled file .
Directly return the file content.
}
zend_op_array *op_array;
if(op_array){
Save op_array;
}
return op_array;
}
{
old_execute(op_array TSRMLS_DC); //Simple execution by the default execution function.
}