使用 bcompiler 來編譯(加密)您的 PHP 原始碼

Using bcomipler to compile (encode) your PHP script (in FreeBSD)

eAccelerator 的 encoder 在 PHP 5.1 以後就無法運作了,使是最新的版本 0.9.5 也不行,因此如果您還想用 eAccelerator 的 encoder,乖乖待在 PHP 4.x or PHP 5.0 吧,相關資訊可參考之前的兩篇文章:

根據 mailing list 上面作者最新的說法,短期內應該都還不會修復,甚至有可能還會放棄支援 encoder 部份,作者還推薦大家改用 bcompiler … orz
所以如果你跟我一樣必須要用 PHP 5.1 的話,就跟我一起來玩看看 bcompiler

附帶一提,原本是要買 ionCube 公司開發的 PHP Encoder 的,這看來是目前編譯(加密)PHP 原始碼最好的選擇,價格也不算貴,而且花的是公司的錢,不過還是先試用看看 bcompiler 好了,真的不好用的話,再來買囉

以下的安裝與使用說明均以 FreeBSD 為範例,其他作業系統應該也差不多,請自己試看看吧

在正式安裝之前提醒你,bcompiler 會用到 bzip2 這個 extension,所以先安裝一下 php5-bz2:

# cd /usr/ports/archivers/php5-bz2
# make install clean

bcompiler 是一個收容於 PECL 的 PHP extension,您可以直接使用 pecl 這個命令來安裝,而 pecl 的使用方法與 pear 相同,因此你必須先安裝 pear :

# cd /usr/ports/devel/pear
# make install clean

pear 安裝好後,同時會有一個 pecl 命令可用 (/usr/locel/bin/pecl),可用它來安裝 bcompiler

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

不過呢,我發現直接執行 pecl 會發生錯誤(至少在 FreeBSD 下是如此):

# pecl
Fatal error: Call to undefined function preg_match() in ...

問題出在 /usr/local/bin/pecl 這個 script 的最後一行:

exec $PHP -C -n -q $INCARG -d output_buffering=1 -d safe_mode=0 ...

這一行的 -n 這個選項代表不載入 php.ini 設定檔,因此 pcre.so 就沒有被載入,
然後你就看到上面的錯誤了,所以自己去改一下程式把 -n 拿掉吧….

拿掉之後,安裝程式應該就可以正常運作了了:

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

安裝好後,記得要改設定把 bcompiler 這個 extension 載入

# echo "extension=bcompiler.so" >> /usr/local/etc/php/extensions.ini

bcompiler 全名是 PHP bytecode Compiler,詳細的使用方法可以參考 PHP 網站上的詳細說明: PHP bytecode Compiler: http://www.php.net/manual/en/ref.bcompiler.php

看不懂的話,bcompiler_write_file() 這個 function 的說明裡面有範例:

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

為了易於使用,我寫了一個簡單的 script,使用方法如下:

BENCODER v1.3 - Encode your PHP script using bcompiler

Usage: bencoder [-f] [-q] -o FILE    file1.php
       bencoder [-f] [-q] -o OUTDIR  file1.php file2.php ...
       bencoder [-f] [-q] -o OUTDIR  -s SRCDIR  [-e SUFFIX] [-r] [-c] [-l]

  -o FILE   : the file name to write the encoded script
              (default to '-encoded.XXX' suffix)
  -o OUTDIR : the directory to write all encoded files

  -a SRCDIR
  -s SRCDIR : encode all files in this source directory

  -r        : encode directories recursively (no by default)
  -f        : force overwriting even if the target exists
  -e SUFFIX : encode the files with the SUFFIX extension only (default: php)
              (regular expression allowed, ex: "php|inc")
  -c        : copy files those shouldn't be encoded (no by default)
  -l        : follow symbolic link (no by default)
  -q        : do not print the file name while encoding or copying
  -b
  -bz2      : compress the encoded files with bz2 (needs bzip2-extension)

想用的人到 這邊 自己抓回去玩玩看吧

# wget http://bencoder.urdada.net/bencoder
# chmod 755 bencoder
# mv bencoder /usr/local/bin/

在把程式使用 bcompiler 編碼之後,執行的時候發覺 $_SERVER 以及 $_ENV 的變數都不見了,後來查了資料,才發現要把 auto_globals_jit 關掉 (Off) 才行,請直接修改您的 php.ini :

; When enabled, the SERVER and ENV variables are created when they're first
; used (Just In Time) instead of when the script starts. If these variables
; are not used within a script, having this directive on will result in a
; performance gain. The PHP directives register_globals, register_long_arrays,
; and register_argc_argv must be disabled for this directive to have any affect.
auto_globals_jit = Off

在〈使用 bcompiler 來編譯(加密)您的 PHP 原始碼〉中有 10 則留言

  1. hsu

    您好 ! 拜讀您的bcompiler文章之後, 使用非常愉快 😛

    以下有個小小問題, 還請您撥冗回應,感恩~
    當使用bencoder使用 -b 這參數將編碼的檔案壓縮變小,
    是否會影響執行效能? 您的建議是?

    回覆
  2. S.V. Sureshkumar

    While running bencoder at command line, the following error message ‘bash: /usr/local/bin/bencoder: /usr/local/bin/php: bad interpreter: No such file or directory’ please give me a solution in English as I am from India

    S.V.Sureshkumar

    回覆
  3. S.V. Sureshkumar

    I am from India. Command line running of bencoder gives the following message bash: /usr/local/bin/bencoder: /usr/local/bin/php: bad interpreter: No such file or directory. Please give me a solution to run bencoder successfully

    S.V.Sureshkumar

    回覆
  4. David Jiang

    It seems that your php are not found. Try to locates installation path of php and modifies the script.

    回覆
  5. calos

    您好: 很感謝您提供這些教學與程式給大家.
    但是我使用後發現:
    PHP5 的物件寫法, 繼承等等, compiler 後執行會有問題, 純 function / include 等等, 是正常的.

    不知道這部份是都這樣還是, 是那邊設定有需要改呢?

    回覆
  6. 自動引用通知: windows + bcompiler | 工作達人(Job Da Ren)

  7. 自動引用通知: automaty do kawy

  8. 自動引用通知: will speak

dada 發表迴響取消回覆