Compiling PHP Scripts

From Phpmaniac

Jump to: navigation, search

Return to Contents

Contents

What is Compiling

Compiling is the process of turning your source code into bytecode which has several benefits, including up to a 30% speed gain as your code will not have to be compiled at run-time and also your source code will be protected.

bcompiler is an open source PHP compiler which has the added benefit of it's compiled scripts being able to run on servers that do not have bcompiler installed.

It is important to remember that even though the original source is not recoverable from the compiled script, data can still be recovered from the compiled script, so it is imperative that passwords that should be secret are not placed in compiled scripts.

Installation

Installation can be done either through your operating systems package manager, or through PECL (PHP Extension Community Library).

Pecl Install

Compile and install the binary in one step using the following command.

pecl install channel://pecl.php.net/bcompiler-0.8

Add the following line to your php.ini

extension=bcompiler.so

Linux

Use the PECL install detailed above

Windows

Download the windows binary http://pecl4win.php.net/ext.php/php_bcompiler.dll for you version of PHP and place it in your extensions directory,

Add the following line to your php.ini

extension=php_bcompiler.dll

FreeBSD

Using packages

Install the binary in one step using the following command.

# pkg_add -r pecl-bcompiler

Add the following line to your php.ini (usually /usr/local/etc/php.ini)

extension=bcompiler.so

Using Ports

Compile and install the binary in one step using the following commands.

cd /usr/ports/devel/pecl-bcompiler
make install clean

Compiling your first script

This script takes the source a source PHP script called in.php, and outputs a compiled PHP script called out.php

  1. <?php
  2.         $fp = fopen("out.php", "w");         // Create the destination PHP file (in this case called out.php)
  3.         bcompiler_write_header($fp);         // Write a bcompiler header in the destination file
  4.         bcompiler_write_file($fp,"in.php");  // Compile the contents of the source file and place in destination file
  5.         bcompiler_write_footer($fp);         // Write the bcompiler footer in the destination file
  6.         fclose($fp);                         // Close the destination file
  7. ?>

in.php

The contents of in.php

  1. <?php
  2.   print("Hello Wrold!");
  3. ?>

out.php

The contents of out.php

^P^@^@^@bcompiler v0.14s        ^B^@^@^@^@\xff\xff\xff\xff^A^@^@^@^C^@^@^@^C^@^@^@(^H^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@^F^L^@^@^@Hello World!^L^@^@^@^A^B^@^H^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^B^@^@^@>^H^@^@^@^@^@^@^@^
@^@^@^@^A^@^@^@^A^A^@^@^@^A^B^@^H^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^D^@^@^@\x95^H^@^@^@^@^@^@^@^@^@^@^@^H^@^@^@^@^@^@^@^@^@^@^@^H^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^D^@^@^@^@^@^@^@^@^@^@^@\xff\xff\xff\xff^@^@^@^A?^@^@^
@bcompiler/in.php\xff\xff\xff\xff^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@

Troubleshooting

No Stable version of bcompiler

# pecl install bcompiler
Failed to download pecl/bcompiler within preferred state "stable", latest release is version 0.8, stability "beta", use "channel://pecl.php.net/bcompiler-0.8" to install
Cannot initialize 'channel://pecl.php.net/bcompiler', invalid or missing package file
Package "channel://pecl.php.net/bcompiler" is not valid
install failed

If you get this message it's because you have selected to run stable PECL packages, as this extension is still experimental, you will need to run the following command to install a beta version

# pecl install channel://pecl.php.net/bcompiler-0.8

Return to Contents

Personal tools