Cashing PHP compilation Execution detach (separating compilation and execution)

Source: Internet
Author: User
Implementing the PHP compilation Execution separation (separating compilation and execution)

Just in the PHP group and we chat, promised everyone to write an article on how to implement the PHP source code encryption, through this will be the opportunity for QA in the smoke, on this issue, I write some ideas.
As I've described in previous articles, ZE (Zend engine) executes a PHP script that goes through compile-and-execute, except that it compiles PHP files every time it executes. The compilation and execution separation is not implemented.
There are two important functions in the compilation and execution phase of ze:
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 the OP codes consisting of the basic instruction sequence of ze, and then handing the OP codes to Zend_execute to get the results of our script.
So, we can completely modify the default Zend_complie_file and Zend_execute to achieve, PHP execution and compilation separation, further, we can also be implemented on this basis, the encryption and decryption of our script.
We do this with a PHP extension module, first of all we need to initialize the module:
Php_minit_function (sample)
{
? old_compile_file = Zend_compile_file; Save site
? old_execute = Zend_execute;
? zend_compile_file = My_compile_file; Intercepted
? zend_execute = My_execute;
? return SUCCESS;
}
In our my_compile_file, we judge if 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)
{
? if (Strstr (file_handle->filename, ". Ze")! = NULL) {
?? is a compiled file. Returns the contents of the file directly.
?}
? Zend_op_array *op_array;
? op_array = Old_compile_file (file_handle, type TSRMLS_CC); Call the default compile, intercept the output
? if (Op_array) {
?? Save Op_array;
?}
? return Op_array;
}
In this way, we have implemented support for compiled files, and for file compilation.
Then, we need to write our execution function:
static void My_execute (Zend_op_array *op_array tsrmls_dc)
{
? Old_execute (Op_array tsrmls_dc); Simply by default execution function execution.
}
Perhaps you want to ask why to packaging after the execution function, oh, I just to illustrate, a way, is can intercept this stuff just. What's the use? Just look at what your readers are asking you to accomplish in this way:).
Write here, you may understand, if you want to encrypt the file, then define an encrypted file type, such as *.zec, and then in My_compile_file, determine the file type, if it is encrypted file, then perform decryption, hey, simple?
As for how to encrypt, it is necessary to ask yourself, you want to use what way, but, remember, to reversible OH ~ ~ ^_^.

?

Original address: http://www.wangchao.net.cn/bbsdetail_1887829.html

  • 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.