Implement PHP compilation and execution separation (separatingcompilationandexecution_PHP tutorial-php Tutorial

Source: Internet
Author: User
Implements the separation of PHP compilation and execution (separatingcompilationandexecution. Chatting with everyone in the PHP Group just now, we should assume that everyone wants to write an article on how to implement PHP source code encryption. with this opportunity, QA is in smoke, I want to write some ideas. Me

Chatting with everyone in the PHP Group just now, we should assume that everyone wants to write an article on how to implement PHP source code encryption. with this opportunity, QA is in smoke, I want to write some ideas.

As I have mentioned in previous articles, ZE (Zend engine) will execute a PHP script that will undergo compilation-> execution, but it will re-compile the PHP file every time it is executed. Compilation and execution separation are not implemented.

In the compilation and execution phases 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 consisting of the basic command sequence of ZE, and then handing the op codes to zend_execute for execution to get the result of our script.

Therefore, we can modify the default zend_complie_file and zend_execute to implement PHP execution and compilation separation. Further, we can implement the encryption and decryption of our scripts on this basis.

We use a PHP extension module to implement this function. First, we need to initialize the module:

PHP_MINIT_FUNCTION (sample)
{
Old_compile_file = zend_compile_file; // save the field
Old_execute = zend_execute;

Zend_compile_file = my_compile_file; // Intercept
Zend_execute = my_execute;
Return SUCCESS;
}

In my_compile_file, determine whether our file is a compiled file. assume that the suffix is *. ze.

Static zend_op_array * my_compile_file (zend_file_handle * file_handle, int type TSRMLS_DC)
{
If (strstr (file_handle-> filename, ". ze ")! = NULL) {// It is a compiled file.
Directly return the file content.
}
Zend_op_array * op_array;

Op_array = old_compile_file (file_handle, type TSRMLS_CC); // call the default compile to capture the output

If (op_array ){
Save op_array;
}
Return op_array;
}

In this way, we have achieved support for Compiled files and for file compilation.

Then, we need to compile our execution function:

Static void my_execute (zend_op_array * op_array TSRMLS_DC)
{
Old_execute (op_array TSRMLS_DC); // simple execution by default.
}

Maybe you have to ask why we need to wrap the subsequent execution functions. haha, I just want to explain that one way is to intercept this stuff. What is the purpose? Let's see what requirements you have for readers :).

Here, you may understand that if you want to encrypt a file, you need to define an encrypted file type, such *. zec, and then judge the file type in my_compile_file. if the file is encrypted, perform decryption. Hey, is it simple?

As for how to encrypt the data, you need to ask yourself, what method do you want to use, but remember, it must be reversible ~~ ^_^.

Bytes. I...

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.