Home php教程 php手册 实现PHP的编译执行分离(separating compilation and execution

实现PHP的编译执行分离(separating compilation and execution

Jun 13, 2016 am 10:34 AM
and php separation exist accomplish implement of compile

刚刚在PHP群内和大家聊天,应承了大家要写一个关于如何实现PHP源码加密的文章,借着这会QA在冒烟的机会,就这个问题,我写点思路。

我以前的文章介绍过,ZE(Zend engine)执行一个PHP脚本会经历编译->执行,只不过它每次执行都会去重新编译PHP文件。并没有实现编译和执行分离。

在ZE的编译和执行阶段,有俩个重要的函数:

ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC);

ZEND_API void (*zend_execute)(zend_op_array *op_array TSRMLS_DC);

zend_compile_file负责将要执行的脚本文件编译成由ZE的基本指令序列构成的op codes,然后将op codes交由zend_execute执行,从而得到我们脚本的结果。

所以,我们完全可以通过修改默认的zend_complie_file和zend_execute来实现,PHP的执行和编译分离,进一步,我们还可以再这个基础上实现,对我们脚本的加密和解密。

我们通过一个PHP扩展模块来实现这个功能,首先,我们需要在模块初始化的时候:

PHP_MINIT_FUNCTION(sample)
{
    old_compile_file = zend_compile_file;  //保存现场
    old_execute = zend_execute;
 
    zend_compile_file = my_compile_file; //截获
    zend_execute = my_execute;
    return SUCCESS;
}

在我们的my_compile_file中,判断我们的文件是否是编译过的文件,假设后缀名是*.ze。

static zend_op_array *my_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC)
{
    if(strstr(file_handle->filename, ".ze") != NULL){//是编译过的文件。
         直接返回文件内容.
    }
   zend_op_array *op_array;

   op_array = old_compile_file (file_handle, type TSRMLS_CC); //调用默认的compile,截获输出
 
   if(op_array){
       保存op_array;
   }
    return op_array;
}

这样,我们就实现了,对已经编译文件的支持,和对文件编译的支持。

然后,需要编写我们的执行函数:

static void my_execute(zend_op_array *op_array TSRMLS_DC)
{
    old_execute(op_array TSRMLS_DC); //简单交由默认执行函数执行。
}

也许你要问为什么要包装以后的执行函数,呵呵,我只是为了说明,一种方式,就是可以截获这个东东而已。有什么用?就看读者你有什么要求能通过这个方式实现了: )。

写到这里,你也许就明白了,如果想要对文件加密,那么就定义个加密文件类型,比如*.zec,然后在my_compile_file中,判断文件类型,如果是加密文件,那么就执行解密,嘿嘿,简单吧?

至于怎么加密,那就要问你自己了,你想用什么方式,但是,记住,要可逆的哦~~^_^。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles