PHP

Software version5.3
Operating SystemDebian 6
WebsitePHP Website

Introduction

Sooner or later, we all face issues related to protecting the fruit of our intellectual labor. Here’s a way to protect your precious PHP code and improve the execution time of your PHP scripts.

Installation

The compiler is only provided in a development environment, which requires some installation steps before use:

1
2
3
4
5
aptitude update
aptitude upgrade
aptitude install make
aptitude install php5-dev
pecl install bcompiler

Then we’ll modify the php.ini file. We need to add extension=bcompiler.so to the end of your php.ini file:

1
2
cp /etc/php5/apache2/php.ini /etc/php5/apache2/php.ini.old
echo "extension=bcompiler.so" >> /etc/php5/apache2/php.ini

Configuration

1
2
3
> php --php-ini /etc/php5/apache2/php.ini -r "bcompiler_write_header();"
OK : PHP Warning: bcompiler_write_header() expects at least 1 parameter, 0 given in Command line code on line 1
KO : PHP Fatal error: Call to undefined function bcompiler_write_header() in Command line code on line 1

If you get a KO, you need to redo the procedure and make sure that pear-php is properly installed.

Compiling PHP Code

Create a source file:

1
2
3
<?php
echo "it codes and decodes";
?>

Then create a file called compiler.php:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<?php
// the path of the bytecode file that will be created later
$bytecode = "bytecode.php";

// The source file
$codesource = "code.php";

// creation of the compiled file
$fichierbytecode = fopen($bytecode, "w");

// writing the file header;
bcompiler_write_header($fichierbytecode);

// writing the body of the file:
bcompiler_write_file($fichierbytecode, $codesource);

// writing the footer of the file:
bcompiler_write_footer($fichierbytecode);

?>

Run the PHP interpreter in CLI:

1
php --php-ini /etc/php5/apache2/php.ini compiler.php

Replace the links on your html index.php page from “code.php” to the bytecode.php file. The result doesn’t change, but the recipe is unknown :-)

References

PHP Bcompiler documentation

Last updated 08 May 2013, 18:54 CEST. history