Implementing the PHP compilation Execution separation (separating compilation and execution)

Source: Internet
Author: User
Tags php script php source code

Reprinted from the Bird's cousin blog: http://www.laruence.com/2008/08/14/250.html

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.

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; Intercept    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);//simple execution by default execution function. }

  

Perhaps you want to ask why to packaging after the execution function, hehe, I just to illustrate, a way, is can intercept this stuff just. What is the use, just look at the reader you have what requirements can be achieved through this way;)

Well, 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, Jane answer?

As for how to encrypt,,, it is necessary to ask yourself, you want to use what way,, but, remember, to reversible OH ~ ~ ^_^.

Implementing the PHP compilation Execution separation (separating compilation and execution)

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.