缩小/混淆 PHP 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2064973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:52:51  来源:igfitidea点击:

Minify / Obfuscate PHP Code

phpcode-generationobfuscationminifyhaxe

提问by TheHippo

I use Haxeto generate PHP code. (This means you write you code in the Haxe language and get a bunch of php files after compiling.) Today a customer told me that he needs a new feature on a old project made with Haxe. He also told me that he altered some small things on the code for his own needs. Now I first have port his changes to my Haxe code and then add the new feature, because otherwise his changes will be overwritten by the next time I compile the project.

我使用Haxe生成 PHP 代码。(这意味着你用 Haxe 语言编写代码,编译后得到一堆 php 文件。)今天一位客户告诉我,他需要一个用 Haxe 制作的旧项目的新功能。他还告诉我,他根据自己的需要对代码进行了一些小改动。现在我首先将他的更改移植到我的 Haxe 代码中,然后添加新功能,否则他的更改将在我下次编译项目时被覆盖。

To prevent that this happens again I am looking for some kind of program that minifies / obfuscates the PHP code. The goal is to make the code unreadable / uneditable as possible.
The ideal tool would run under Linux and could process whole folders and all it containing files.

为了防止这种情况再次发生,我正在寻找某种可以缩小/混淆 PHP 代码的程序。目标是使代码尽可能不可读/不可编辑。
理想的工具将在 Linux 下运行,并且可以处理整个文件夹及其包含的所有文件。

Anybody any suggestions?

有人有什么建议吗?

回答by RoboTamer

Why not use the php buid in function php_strip_whitespace()

为什么不在函数php_strip_whitespace() 中使用 php buid

string php_strip_whitespace ( string $filename )

Returns the PHP source code in filename with PHP comments and whitespace removed. This may be useful for determining the amount of actual code in your scripts compared with the amount of comments. This is similar to using php -w from the commandline.

返回文件名中的 PHP 源代码,并删除 PHP 注释和空格。这对于确定脚本中的实际代码量与注释量相比可能很有用。这类似于从命令行使用 php -w 。

回答by J. Martin

I agree with the comment, what you are doing is very underhanded, but after 10 years in this biz I can attest to one thing: Half the code you get is so convoluted it might as well have been minified, and really function/var names are so often completely arbitrary, i've edited minified js and it wasn't much more of a hassle than some unminified code.

我同意这个评论,你所做的事情非常卑鄙,但是在这个行业工作了 10 年之后,我可以证明一件事:你得到的一半代码太复杂了,它可能已经被缩小了,而且真的是函数/变量名经常是完全随意的,我编辑了缩小的 js,它并不比一些未缩小的代码麻烦多少。

I couldn't find any such script/program, most likely because this is kind of against the PHP spirit and a bit underhanded, never the less.

我找不到任何这样的脚本/程序,很可能是因为这有点违背 PHP 精神并且有点卑鄙,从来没有。

First: Php isn't white space sensitive, so step one is to remove all newlines and whitespace outside of string.

第一:Php 对空格不敏感,所以第一步是删除字符串之外的所有换行符和空格。

That would make it difficult to mess with for the average tinkerer, an intermediate programmer would just find and replace all ;{} with $1\n or something to that effect.

对于普通的修补匠来说,这将使其难以解决,中级程序员只需找到并替换所有 ;{} 为 $1\n 或类似效果的东西。

The next step would be to get_defined_functions and save that array (The 'user' key in the returned array), you'll need to include all of the files to do this.

下一步是 get_defined_functions 并保存该数组(返回数组中的“user”键),您需要包含所有文件才能执行此操作。

If it's oo code, you'll need get_defined_classes as well. Save that array.

如果是 oo 代码,则还需要 get_defined_classes。保存该数组。

Essentially, you need to get the variables, methods, and class instances, you'll have to instantiate the class and get_object_vars on it, and you can poke around and see that you can get alot of other info, like Constants and class vars etc.

本质上,您需要获取变量、方法和类实例,您必须在其上实例化类和 get_object_vars,您可以四处查看,看看您可以获得很多其他信息,例如常量和类变量等.

Then you take those lists, loop through them, create a unique name for each thing, and then preg_replace, or str_replace that in all of the files.

然后你获取这些列表,循环遍历它们,为每个事物创建一个唯一的名称,然后在所有文件中使用 preg_replace 或 str_replace 。

Make sure you do this on a test copy, and see what errors you get.

确保在测试副本上执行此操作,并查看会出现什么错误。

Though, just to be clear, there is a special place in hell reserved for people who obfuscate for obfuscation's sake.

不过,要清楚的是,地狱中有一个特殊的地方是为那些为了混淆而混淆的人保留的。

Check out: get_defined_functionsget_declared_classesand just follow the links around to see what you can do.

查看:get_defined_functions get_declared_classes并按照周围的链接查看您可以做什么。

回答by catsby

We use Zend Guardto encode our PHP code with certain clients, but as Parrots said, you need to be sure youown the code. We only encode in certain situations, and only when it's explicit that we retain ownership of the code, otherwise Parrots is right, the client has a right to modify it.

我们使用Zend Guard对某些客户端的 PHP 代码进行编码,但正如 Parrots 所说,您需要确保拥有这些代码。我们只在某些情况下进行编码,并且只有在我们明确保留代码所有权的情况下才进行编码,否则 Parrots 是对的,客户有权对其进行修改。

回答by Parrots

I know of Zendguard, Expressionengine used it to encrypt their trial version's core code. You could always give that a go although you need to pay for it.

我知道Zendguard,Expressionengine 用它来加密他们试用版的核心代码。尽管您需要为此付费,但您始终可以试一试。

However, while I understand the frustration of having to port his changes, I assume they purchased the code from you? They have the right to modify it. You just have the right to charge them extra to port their changes ;) Imagine if you stopped working for them, how could they ever hire someone else to update the code?

但是,虽然我理解不得不移植他的更改的挫败感,但我认为他们从您那里购买了代码?他们有权修改它。您只是有权向他们收取额外费用以移植他们的更改;) 想象一下,如果您停止为他们工作,他们怎么可能聘请其他人来更新代码?

回答by Ira Baxter

Our PHP Obfuscatordoes exactly the the job of stripping comments, whitespaces, and scrambling identifiers.

我们的PHP Obfuscator完全可以完成剥离注释、空格和加扰标识符的工作。

It operates across a complete set of PHP files to ensure that scrambled symbols are scrambled consistently across those files, ensuring correct operation even after scrambling.

它在一组完整的 PHP 文件中运行,以确保在这些文件中一致地对加扰符号进行加扰,从而确保即使在加扰后也能正确操作。

EDIT 2013: Now encrypts string literals to make them unreadable. Operates under Windows, and on Linux under Wine.

编辑 2013:现在加密字符串文字以使其不可读。在 Windows 下运行,在 Wine 下在 Linux 上运行。

回答by Alix Axel

回答by andrew

I have just find minify-service for PHP. It's really looks usefull. They says, that obfuscating will be available soon. I hope this is true :) http://customhost.com.ua/php-minify/

我刚刚找到了 PHP 的 minify-service。看起来真的很管用。他们说,混淆将很快可用。我希望这是真的:) http://customhost.com.ua/php-minify/