Welcome to a guide on how to encrypt and hide PHP source code. Need to hide the source code for security reasons? Or maybe a client is not willing to pay in full? Sadly, there are no “smart solutions” to hide PHP code.
It is impossible to totally hide the PHP source code since it is an interpreted language, but there are a few possible alternatives:
- Use a code obfuscator to make the source code difficult to read.
- Use a code protector or encoder. Something like an alternative PHP engine, but with the ability to protect the source code.
- Lastly, compile the PHP code using a converter or virtual machine.
Either way, hiding the source code is not going to be straightforward. I will not recommend doing so, as you might end up wasting time and money instead. But if you really want to do it, this guide will walk you through the possible options – Read on!
TABLE OF CONTENTS
|Free Obfuscator||Paid Encoders||PHP Compilers|
|Useful Bits||The End|
METHOD 1) FREE PHP OBFUSCATORS
The first alternative is to use one of the many free PHP obfuscators. Just what is an obfuscator? It simply turns the original code into a mess of a seemingly jumbled script that cannot be easily read by humans.
Do a search online for “free PHP obfuscator”, and you will get a number of different services to choose from. One of them is Mobile Fish, which we can copy and paste our PHP code into. For example, when we input the following script:
<?php $varA = "Hello World"; $varB = "Foo Bar"; $varC = $varA . " " . $varB; echo $varC;
That will give us a very surprising:
Yep, don’t think that is human-readable anymore.
Most online obfuscators have limitations on the number of characters, so if you have massive scripts, it is better to download and use an offline obfuscator instead. There are a couple of good ones out there, and one that I will recommend is YAK Pro. You can either download it straight from Github or clone it directly – It does require the PHP-Parser library as well.
git clone https://github.com/pk-fr/yakpro-po.git git clone https://github.com/nikic/PHP-Parser.git
Once you have downloaded both libraries, you can run YAK Pro in the command line to encode your scripts:
D:\http\yakpro-po-master>php yakpro-po.php test.php Info: Using [D:\http\yakpro-po-master\yakpro-po.cnf] Config File... Info: yakpro-po version = 2.0.3 Info: Process Mode = file Info: source_file = [D:\http\yakpro-po-master\test.php] Info: target_file = [stdout] Obfuscating D:\http\yakpro-po-master\test.php <?php /* __________________________________________________ | Obfuscated by YAK Pro - Php Obfuscator 2.0.3 | | on 2019-03-09 09:25:54 | | GitHub: https://github.com/pk-fr/yakpro-po | |__________________________________________________| */ goto LLSso; mh5vK: $EjslL = $ZvlM7 . "\x20" . $c23ok; goto xEHTG; LLSso: $ZvlM7 = "\x48\x65\154\x6c\x6f\x20\127\x6f\162\154\144"; goto uktyA; uktyA: $c23ok = "\106\x6f\x6f\x20\x42\141\162"; goto mh5vK; xEHTG: echo $EjslL; Info: [variable ] scrambled : 3 Info: [function ] scrambled : 0 Info: [method ] scrambled : 0 Info: [property ] scrambled : 0 Info: [class ] scrambled : 0 Info: [class_constant] scrambled : 0 Info: [constant ] scrambled : 0 Info: [label ] scrambled : 4
P.S. I have set Yak Pro to directly output the code in the command line here. But it does accept quite a lot of configuration parameters, and you can specify where to save the obfuscated code to. Please do check out their official webpage for more documentation.
NOTES & LIMITATIONS
- Obfuscators work on the basis of hashing the original script.
- It is still possible to reverse engineer and get back the original script.
- Obfuscated scripts are not good performance-wise. It has to decode the hashed string first, then run
- The level of protection and performance depends on the encoding-decoding algorithm.
METHOD 2) PAID PHP PROTECTORS & ENCODERS
Don’t trust the free PHP obfuscators? Or need something more professional? Then there are a few paid PHP code protectors and encoders available. Be warned though, they can cost quite a lot.
THE PHP PROTECTORS & ENCODERS
THE BEST ENCODER?
Personally, I have not gone deep into the paid options – They are expensive! But Zend Guard looks good, and even comes with a licensing system… Not a sponsored post, but check out their quick start tutorial if you are interested:
NOTES & LIMITATIONS
The performance of “code protectors” varies, it really depends on how the engine works. Some can be simple obfuscators as above, while a few others might be an “alternative PHP engine”, or “PHP BlackBox extension”… Hence the price.
METHOD 3) PHP COMPILER
This final option might cause some code ninjas to cringe, as it involves compiling PHP code and breaking the “traditional” way of how things work.
INTERPRETER VS COMPILER
A quick recap for you beginners who might have forgotten the difference between interpreters and compilers:
- Compiled languages: The source code needs to be compiled into a self-contained executable first. For example, we can create executable files with C++; The executable files can run independently on computers without having to install C++.
NOTES & LIMITATIONS
PHP is an interpreted language “by default” and there is no official way for us to compile PHP scripts. Some of the “PHP compilers” available out there work by converting PHP to another language (such as Java), then compiling it.
In this case, the compiled scripts will officially turn into a standalone executable – No longer readable by PHP engines nor web servers as you might originally intend. So please do take extra note of this aspect, “compiled PHP” scripts are mostly meant for self-contained command-line applications only.
THE PHP COMPILERS
There are plenty of “PHP compilers”, and you can check these out:
That’s all for the options, and here is a small section on some extras that may be useful to you.
WHICH IS THE BEST?
It depends on the situation and what you want.
- Free obfuscators don’t perform that well, but they protect the code well enough and cost nothing.
- Paid encoders might not perform that well either, but they do offer a certain level of protection.
- Compilers are not 100% compatible, do not produce website-friendly scripts, but they perform very well… and trying to reverse engineer a compiled script is virtually impossible.
TO ENCODE OR NOT TO ENCODE
It all boils down to your original intention. If you have a million-dollar system, and security is a concern – Then yes, spending some time and money to protect your intellectual property is well worth all the effort.
But if you are facing a problem with non-paying customers, it may still be a good idea to free obfuscate some of the main library files – Don’t waste too much time, money, and let the non-paying customer suffer the performance loss. Keep the full source code and improved performance as a bargaining chip – Just move on with your next project and customer.
Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!