Drive your php code viewers mad! Encode your php script

If you are among the ones who give away free self written php scripts to everybody but want some way to stop script kiddies and newbies just copy-pasting and modifying your script or you just dont want code newbies to know the original code then this post is probably for you. The trick is to encode your php code as a string and then parse the encoded string as php code after decoding it. This trick is very commonly usede by developers. Though this trick cannot stop code copying but can discourage the viewer to a certain extent to copy the code.
Here is how to do it:

  1. Encode your code as string using any encoding method/functions . the most commonly used is base64_encode. You can even use Dynamicguru’s Online base 64 encoder and decoder to instantly convert your code to base64 format
  2. In your script file, decode the generated string using base64_decode function and assign it to a variable
  3. Now call the function eval() passing your decoded string as argument and you are done!

Example :

Consider the following piece of code :

<?php
echo "hello world";
?>

Now suppose you want to disguise the code and make it look un-interpretable , Encode it using our Online base 64 encoder and decoder . here is what the code looks like after encoding :

PD9waHANCmVjaG8gXCJoZWxsbyB3b3JsZFwiOw0KPz4=
//Amazing isnt it?

Now call the eval() function :

eval(base64_decode("PD9waHANCmVjaG8gXCJoZWxsbyB3b3JsZFwiOw0KPz4= "));

//the above code is equal to :

<?php
echo "hello world";
?>
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Note
This trick should not be used if you want to completely disguise your code, as anybody can go a step further and decode it using base64_decode function.

The eval fucntion evaluates a string as a php code and is of immense help in situations where you want a piece of code to be stored in a file / database and executed at a later time

hope you enjoyed the post and learnt something new and useful :D

2 Responses to this entry

Leave a Reply