PHP opcache存储已编译文件,源码保护

一、配置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、项目实际放的目录,要与缓存的目录一致

 

转载地址

posted @ 2023-09-21 17:05  菜的掉渣  阅读(203)  评论(0编辑  收藏  举报