PHP opcache儲存已編譯檔案,原始碼保護
阿新 • • 發佈:2020-12-14
一、配置opcache
zend_extension=opcache
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
;opcache不儲存註釋,減少opcode大小
opcache.save_comments=0
;關閉PHP檔案時間戳驗證
opcache.validate_timestamps=Off
;每60秒驗證php檔案時間戳是否更新
;opcache.revalidate_freq=60
opcache.fast_shutdown= 1
;注意,PHP7下命令列執行的指令碼也會被 opcache.file_cache 快取.
opcache.enable_cli=1
;設定不快取的黑名單
;opcache.blacklist_filename="E:\phpstudy_pro\WWW\dudu\opcache_compile_file.php"
;快取檔案位置
opcache.file_cache="E:\phpstudy_pro\Extensions\opcache"
opcache.file_cache_only=0
opcache.enable=On
二、程式碼編譯
1、PHP執行如下檔案內容opcache_compile_file.php
<?php
function getfiles( $path , &$files = array() ) {
if ( !is_dir( $path ) ) return null;
$handle = opendir( $path );
while ( false !== ( $file = readdir( $handle ) ) ) {
if ( $file != '.' && $file != '..' ) {
$path2 = $path . '/' . $file;
if ( is_dir( $path2 ) ) {
getfiles( $path2 , $files );
} else {
if ( preg_match( '%\.php$%' , $file ) ) {
$files[] = $path2;
}
}
}
}
return $files;
}
echo 'aaa';
var_dump(__DIR__);
// 獲取當前目錄及其子目錄下的所有PHP檔案
$files = getfiles(__DIR__);
foreach($files as $file){
// @TODO 中間有不能編譯的程式碼就會停止,所以要排查出錯的程式碼
opcache_compile_file($file); //編譯PHP檔案生成opcode
file_put_contents($file, ''); //清空原來的PHP指令碼
echo $file."\n";
}
echo 'Total PHP Files: '.count($files)."\n";
?>
三、程式碼移植
1、專案實際放的目錄,要與快取的目錄一致
轉載地址