在CentOS服務器上配置ThinkPHP安全防護,可以參考以下步驟:
更新系統和軟件包:
sudo yum update -y
關閉錯誤報告:
display_errors
設置為 Off
:display_errors = Off
配置PHP安全設置:
memory_limit = 256M
max_execution_time = 30
輸入過濾與驗證:
$data = $Request->post();
$validate = Validator::make($data, [
'username' => 'require|max:25|min:3',
'email' => 'require|email'
]);
if (!$validate->check()) {
$this->error('參數錯誤');
}
SQL注入防護:
$user = Db::name('users')->where(['username' => $username])->find();
文件上傳安全:
$file = $Request->file('file');
$info = $file->validate(['size' => 1024*1024*2, 'ext' => 'jpg,png,gif']);
if (!$info) {
$this->error('文件大小或類型錯誤');
}
$file->move(public_path('uploads'));
使用安全的會話管理:
'type' => 'file',
'expire' => 1800,
'encrypt' => true,
關閉調試模式:
app_debug = false
設置安全的文件權限:
sudo chmod -R 755 /path/to/your/thinkphp/project
定期更新和維護:
composer update