Minify/Obfuscate PHP Code

Minify / Obfuscate PHP Code

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.

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.

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

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.

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.

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

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.

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.

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_functions get_declared_classes and just follow the links around to see what you can do.

Obfuscate javascript with inline PHP?

Not aware of any obfuscator capable of doing this, but you could simply make your JavaScript code reference a Config Object instead of the PHP code. Then you can obfuscate the main JavaScript code, e.g.

// Config object with anything that has to be assigned through PHP
var Config = { 'foo': '<?php echo $foo?>' }

// and some obfuscated code that uses the Config object
var _0x76dc=["\x66\x6F\x6F"];alert(Config[_0x76dc[0]]);

Javascript minification and shrinking variables with PHP

exec('java.exe -jar compiler.jar --js pre.js --manage_closure_dependencies true --js_output_file end.js');

exec('java.exe -jar yuicompressor.jar pre.js  -o end.js');

use:

google closure compiler AND
http://developer.yahoo.com/yui/compressor/

PhpStorm compress code into one big mess

PHPStorm doesn't have any built-in functions for this. You might want to checkout phpprotector or YUI Compressor

Is PHP minification helpful?

Minifying php is pointless from a network bandwidth perspective, minifying the output of the php might be beneficial however as the html that is output is transmitted. The original php will be hard to maintain if it's minified. Which is also the point of obsfucation. If someone gets hold of your php, then it will prevent them knowing what it does without more effort...



Related Topics



Leave a reply



Submit