application/config/config.php 回傳一個 $config 陣列,框架啟動時自動載入。
<?php
$config['base_url'] = 'https://www.example.com/'; // 必須以 / 結尾
// 自動偵測(不推薦生產環境)
$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? 'https' : 'http')
. '://' . $_SERVER['HTTP_HOST']
. str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
用途:site_url()、base_url()、表單 action、圖片路徑等所有連結生成基準。
<?php
$config['index_page'] = ''; // 隱藏 index.php,需配合 .htaccess
// $config['index_page'] = 'index.php'; // 預設
$config['uri_protocol'] = 'REQUEST_URI'; // 推薦:支援 PATH_INFO、QUERY_STRING
// 可選:'PATH_INFO', 'QUERY_STRING', 'REQUEST_URI', 'ORIG_PATH_INFO'
$config['url_suffix'] = '.html'; // 偽靜態後綴,生成 /page.html
// $config['url_suffix'] = ''; // 無後綴
<?php
$config['language'] = 'english'; // 語系目錄 application/language/english/
$config['charset'] = 'UTF-8'; // 統一專案編碼,資料庫、HTML、PHP 皆設為 UTF-8
<?php
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-'; // URL 允許字元正則,嚴格限制防注入
// $config['permitted_uri_chars'] = ''; // 允許所有(危險!)
$config['enable_hooks'] = true; // 啟用鉤子,擴展核心不修改系統檔
$config['global_xss_filtering'] = false; // 不建議全域開啟,效能影響大
// 控制器/模型中針對性使用 $this->security->xss_clean()
$config['csrf_protection'] = true; // CI 3 內建 CSRF
$config['csrf_token_name'] = 'csrf_token';
$config['csrf_cookie_name'] = 'csrf_cookie';
$config['csrf_expire'] = 7200; // 2 小時
$config['csrf_regenerate'] = true; // 每次請求更新 Token
<?php
$config['log_threshold'] = 1; // 1=錯誤, 2=除錯, 3=資訊, 4=全部, 0=關閉
// 生產建議 1,開發建議 2 或 4
$config['log_path'] = ''; // 空則用 application/logs/
$config['log_date_format'] = 'Y-m-d H:i:s';
$config['cache_path'] = ''; // 空則用 application/cache/
// 確保目錄可寫:chmod 755 application/cache application/logs
<?php
$config['encryption_key'] = 'your-32-char-random-key-here'; // 必須設定,用於加密庫
// 產生:php -r "echo bin2hex(random_bytes(32));"
$config['sess_driver'] = 'files'; // files, database, redis, memcached
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = NULL; // files 時為路徑,database 時為表名
$config['sess_match_ip'] = false;
$config['sess_time_to_update'] = 300;
<?php
$config['compress_output'] = true; // 啟用 GZIP 輸出壓縮
// 需 PHP zlib 擴展,自動對支援的瀏覽器壓縮
$config['time_reference'] = 'local'; // 'local' 或 'gmt'
$config['rewrite_short_tags'] = false; // 短標籤 由 CI 重寫,不建議依賴
<?php
$config['proxy_ips'] = '10.0.0.1,10.0.0.2'; // 信任的代理 IP,用於 $this->input->ip_address()
// 逗號分隔,支援 CIDR:'10.0.0.0/24'
<?php
// application/config/development/config.php
$config['log_threshold'] = 4;
$config['compress_output'] = false;
// application/config/production/config.php
$config['log_threshold'] = 1;
$config['compress_output'] = true;
$config['csrf_protection'] = true;
// index.php 設定環境
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
| 項目 | 建議值 | 說明 |
|---|---|---|
| base_url | 完整 HTTPS URL | 避免自動偵測失敗 |
| index_page | '' | 配合 .htaccess 隱藏 index.php |
| uri_protocol | REQUEST_URI | 相容性最好 |
| permitted_uri_chars | 嚴格正則 | 防 XSS/注入 |
| encryption_key | 32 字元隨機 | 加密安全基石 |
| csrf_protection | true | 防 CSRF 攻擊 |
| compress_output | true | 減少傳輸體積 |
| log_threshold | 生產 1 | 平衡資訊與效能 |
| proxy_ips | 實際代理 IP | 正確取得客戶 IP |