程式語言 dada on 7月 25, 2006
使用 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
[2009/04/29 update]
bcompiler 提供的文件說明並沒有很詳細說明適用範圍,這個 bencoder 所採用的作法事實上也不是很正確,除了還不能處理 class 之外,include 的語意上也會有點問題
原本 runtime 的 include 會變成 compile-time 的 include
例如 a.php include b.php,compile 之後 a.php 會已包含 b.php, 而不是 runtime 再去 include
某些情況會造成一些預期之外的錯誤
(本文共被觀看 1,589 次)
on 25 May 2008 at 9:22 pm 1.hsu said …
您好 ! 拜讀您的bcompiler文章之後, 使用非常愉快
以下有個小小問題, 還請您撥冗回應,感恩~
當使用bencoder使用 -b 這參數將編碼的檔案壓縮變小,
是否會影響執行效能? 您的建議是?
on 28 Jul 2008 at 1:01 pm 2.S.V. Sureshkumar said …
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
on 28 Jul 2008 at 1:54 pm 3.S.V. Sureshkumar said …
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
on 01 Jan 2009 at 7:32 pm 4.johnny said …
Thanks for good post
on 13 Jan 2009 at 6:32 pm 5.David Jiang said …
It seems that your php are not found. Try to locates installation path of php and modifies the script.
on 13 Jan 2009 at 9:13 pm 6.dada said …
Hi David
Thanks for your reminding.
This script has been relocated at: http://bencoder.urdada.net/
Thanks.
on 27 Apr 2009 at 6:17 pm 7.calos said …
您好: 很感謝您提供這些教學與程式給大家.
但是我使用後發現:
PHP5 的物件寫法, 繼承等等, compiler 後執行會有問題, 純 function / include 等等, 是正常的.
不知道這部份是都這樣還是, 是那邊設定有需要改呢?
on 29 Apr 2009 at 10:58 am 8.dada said …
是的.. bcompiler 提供的文件說明並沒有說明適用範圍,
這個 bencoder 所採用的作法事實上也不是很正確,
除了還不能處理 class 之外,include 的語意上也會有點問題,
原本 runtime 的 include 會變成 compile time 的 include
例如 a.php include b.php,compile 之後 a.php 會已包含 b.php,
而不是 runtime 再去 include
某些情況會造成一些預期之外的錯誤