安裝 PHP bcompile 做 編譯/加密 原始碼

PHP 的程式在公司在交付程式中, 比較麻煩的就是要如何把原始碼編譯或加密, 免費的方案, 可以考慮用 bcompiler(PHP bytecode Compiler).

不過這套於 PHP5 的物件寫法, 繼承等等, compiler 後執行會有問題, 純 function / include 等等, 是正常的.

PHP bcompiler 安裝步驟

下述環境於 Debian / Ubuntu Linux 安裝

  1. apt-get install libbz2-dev php-pear php5-dev # 因為 pecl compile bcompiler 需要 libbz2(bz2 library)
  2. pecl install channel://pecl.php.net/bcompiler-0.8 # 因為 bcompiler 是 beta, 不是 stable, 所以需要完整路徑.
  3. echo "extension=bcompiler.so" >> /etc/php5/conf.d/bcompile.ini
  4. /etc/init.d/apache2 restart

確認 Module 是否載入

  • php -m | grep bcompiler
  • phpinfo(); # 看是否有載入 bcompiler module

PHP bcompiler 使用

先寫一個編譯程式, 準備把 example.php 編譯成 example.phb (或 xxx.php 都可以), 詳細可見: bcompiler_write_file.

編譯其他程式用的檔案 bcompiler.php


<?php
$fh = fopen("example.phb", "w");
bcompiler_write_header($fh);
bcompiler_write_file($fh, "example.php");
bcompiler_write_footer($fh);
fclose($fh);
?>

hello.php


<?php
function hello()
{
   echo "hello\n";
}
?>

example.php


<?php
include('hello.php');
hello();
?>

  1. 編譯: php bcompiler.php # 會發現有產生出 example.phb (或於 bcompiler.php 有另外取名字的 xxx.php)
  2. 執行: php example.phb # 會秀 hello, 然後此檔案已經是編譯過的 php code.

另外查到 使用 bcompiler 來編譯(加密)您的 PHP 原始碼 這篇的作者, 寫了一個 bencoder 的好用程式, 可以 compile 整個目錄, 詳細可見: BENCODER v1.3 - Encode your PHP script using bcompiler

Bencoder 安裝使用

  1. wget http://bencoder.urdada.net/bencoder
  2. chmod +x ./bencoder
  3. vim bencoder # 修改 php 執行路徑, Debian PHP 存放路徑在 /usr/bin/php

    #!/usr/local/bin/php 改成 #!/usr/bin/php

  4. 執行: ./bencoder -f -o php/Breadcrumb_ben/ -s php/Breadcrumb/ -r

    encoded: php/Breadcrumb_ben//first.php
    encoded: php/Breadcrumb_ben//index.php
    encoded: php/Breadcrumb_ben//bread.php
    encoded: php/Breadcrumb_ben//second.php

作者: Tsung

對新奇的事物都很有興趣, 喜歡簡單的東西, 過簡單的生活.

在〈安裝 PHP bcompile 做 編譯/加密 原始碼〉中有 16 則留言

  1. 請問目前 bcompile 還是不能支援 物件寫法, 繼承寫法嗎? 有其他辦法可以解決嗎?
    請不吝指教... 謝謝~

  2. 沒有耶, 免費版我指推薦 bcompile, 不過, 如果會賺錢的話, 我建議還是用付費版得, 畢竟有人維護, 出問題有人可以找, 會比較方便點.

  3. Fatal error: Call to undefined function bcompiler_write_header() in xxxxxx.php on line 3
    是不是代表我主機沒安裝bcompiler,
    不然版主都用什麼加密原始檔??

  4. cat "extension=bcompiler.so" >> /etc/php5/conf.d/bcompiler.ini
    ^^^ cat 應該是 echo
    如果用 cat 的話,應該要用 cat > /etc/php5/conf.d/bcompiler.ini

Tsung 發表迴響取消回覆

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料