1

I would like to encode a php page which contains some php functions.

For example, I have a page named: code.php with this functions:

<?php 
function data(){
echo "foo";
...
}

function storage(){
echo "storage files..";
...
}
?>

I use these functions in my other php pages and I would like to protect them by other users. How can I encode their code? I read about base64_encode() but the examples only show how to encode a string: how can I use this solution to encode and decode my php functions?

Thank you!

Marcus Barnet
  • 2,083
  • 6
  • 28
  • 36

1 Answers1

0

If you want to stop others from seeing your PHP code you can either make it as hard as possible (via minifying, obfuscating, whatever you wish to call it) or encrypt it.

There's an answer right here on SO with a few suggestions and another I'd add is ion cube.

With encrypted code you're likely to need further changes to your web server such as an apache module. With obfuscation it will just make it harder for the other developers to read, for instance changing variables and functions names to something meaningless and hard to read.

You will inevitably need to keep a copy of your unobfuscated PHP so you can work on it in a sane manner, which may be hard if you're only developing on your server.

To use Base64 you're probably thinking of doing something like this:

eval(base64_decode('ZnVuY3Rpb24gZGF0YSgpew0KZWNobyAiZm9vIjsNCn0NCmZ1bmN0aW9uIHN0b3JhZ2UoKXsNCmVjaG8gInN0b3JhZ2UgZmlsZXMuLiI7DQp9DQokZGF0YSA9ICdkYXRhJzsNCiRzdG9yYWdlID0gJ3N0b3JhZ2UnOw=='));

What's happening here is the Base 64 string is actually valid PHP, and you first decrypt it the eval it. An example of what the decoded string might look like:

function data(){
echo "foo";
}
function storage(){
echo "storage files..";
}
$data = 'data';
$storage = 'storage';

After the above eval call you would then do something like:

// call the data function
$data();

// call the storage function
$storage();

As stated from the documentation:

PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it.

So, calling $someVariable() will try to run a function named whatever $someVariable contains. If you set $someVariable to foo, it would try to run foo(), if you set $someVariable to sausage, it would try to run sausage() and so on.

Obviously bear in mind that you need to make sure these function variables' names aren't going to be used elsewhere.

Community
  • 1
  • 1
LeonardChallis
  • 7,759
  • 6
  • 45
  • 76
  • Other users can open and read my php files since they also have access on my local ubuntu server. Since they are not skilled programmers, I was thinking to only obfuscating my php functions. I only need to hide my code somehow. So, base64_encode() is not a good solution for this? – Marcus Barnet Apr 13 '16 at 10:15
  • To satisfy my curiosity, why don't you want other developers to be able to read your code? Anyway, you need to be able to still develop your code, meaning you'll need an unobfuscated version of your files. You'd have to hide them. I've added more details to my answer. – LeonardChallis Apr 13 '16 at 10:18
  • It's because other developers are lazy and sometimes use my code for their own purposes and I dont like this. Thank you for your suggestion! I'm not understanding the use of $data(); and $storage();, can you explain me better please? – Marcus Barnet Apr 13 '16 at 10:31
  • Added further description and a link to the official documentation with more examples. – LeonardChallis Apr 13 '16 at 10:35
  • 1
    @MarcusBarnet You can't stop them from decrypting your code. – Scott Arciszewski Apr 13 '16 at 13:45