/ (系統 10-20G) + /home (數據/代碼/日誌,剩餘空間)<?php
// 執行後台任務、系統工具、跨語言腳本
$output = shell_exec('python3 /scripts/process.py --id=123 2>&1');
$ret = exec('/usr/bin/wkhtmltopdf input.html output.pdf', $out, $code);
// 安全原則
// 1. 絕不直接拼接用戶輸入到命令
// 2. 使用 escapeshellarg() / escapeshellcmd()
// 3. 固定命令白名單,參數化傳遞
$safeArg = escapeshellarg($userInput);
exec("/usr/bin/convert {$safeArg} -resize 200x200 output.jpg");
?>
<?php
// 統一返回結構
function getUser($id) {
// 1. 先查緩存
$cacheKey = "user:{$id}";
if ($data = Redis::get($cacheKey)) return ['code'=>0, 'data'=>json_decode($data,true)];
// 2. 查資料庫
$user = DB::table('users')->find($id);
if (!$user) return ['code'=>404, 'msg'=>'用戶不存在'];
// 3. 寫緩存
Redis::setex($cacheKey, 3600, json_encode($user));
return ['code'=>0, 'data'=>$user];
}
// 錯誤碼規範:0=成功, 4xx=客戶端錯誤, 5xx=服務端錯誤, 業務碼 1000+
?>
$input = $_POST['key'] ?? ''; (不再盲目 addslashes)Validator::make($input, ['key'=>'required|string|max:50'])->validate();htmlspecialchars($str, ENT_QUOTES, 'UTF-8') 防 XSS參考《PHP 高併發鎖機制實作》一文,已在生產環境穩定運行:CacheLock 類支援 EAccelerator/文件雙模式,解決報表生成衝突、緩存重建穿透。
<?php
// 開發環境
error_reporting(E_ALL & ~E_STRICT & ~E_DEPRECATED); // 嚴格模式,強制變數初始化
ini_set('display_errors', 1);
// 生產環境
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', '/var/log/php/error.log');
// 自定義錯誤處理器
set_error_handler(function($severity, $message, $file, $line) {
if (!(error_reporting() & $severity)) return false;
throw new ErrorException($message, 0, $severity, $file, $line);
});
?>