CI框架源码阅读---------Config.php
文件地址:./system/core/Config.php
主要作用:管理配置
1.成员属性$config 所有已加载配置的值的列表
2.成员属性$is_loaded 所有加载配置文件的列表
3.成员属性$_config_paths 当需要加载配置文件的时候搜索路径的列表
4.__construct() 构造方法程序会首先自动执行这个方法
它所做的内容主要有两个 a)获取配置赋值给成员属性$config
b)设置配置中的base_url
5.load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
加载配置文件
$file 是你自定义的配置文件的文件名,这个文件名没有 .php 的扩展名.
$use_sections 如果你需要加载多个自定义配置文件,一般情况下它们会被合并成一个数组。然而,如果在不同的配置文件中存在同名的索引,那么会发生冲突。为了避免这个问题,你可以把第二个参数设置为 TRUE ,这可以使每个配置文件的内容存储在一个单独的数组中,数组的索引就是配置文件的文件名。
$fail_gracefully允许屏蔽当配置文件不存在时产生的错误信息:
(0)过滤并设置$file 变量
(1)初始化$founf 为FALSE 用于判断文件是否存在
(2)初始化$loaded为FALSE 用于判断文件是否被加载
(3)检测文件路径如果有环境变量添加环境变量
(4)进入foreach遍历文件路径,并查找文件是否被加载和存在
(5)如果文件不存在跳出foreach循环
(6)加载文件
(7)判断$config是否存在,这个$config 应该是加载的文件中定义的
(8)判断$use_sections 如果为真则将使每个配置文件的内容存储在一个单独的数组中,数组的索引就是配置文件的文件名。
如果为假则所有配置文件会被合并成一个数组
(9)将文件添加进is_loaded数组中,并销毁$config
(10)$loaded 设置为true log记录 跳出循环
(11)如果loaded为false并且$fail_gracefully不等于true 显示错误日志
6.item() 获取一个配置项
$item 配置项的名字
$index 如果配置是一个数组的时候,这一项是数组索引名字
(1)判断index是否为空
(2)如果index为空,判断$this->config[$item]是否存在,如果不存在返回,存在,赋值给$pref
(3)如果index不为空,判断$this->config[$index]是否存在,判断$this->config[$index][$item]是否存在,将$this->config[$index][$item]赋值给$pref
(4)返回$pref
7.slash_item() 获取一个配置项并添加/
$item 配置项的名字
(1)判断配置项是否存在不存在返回false
(2)判断配置项是否为空如果是空返回''
(3)在配置值的后面添加/并返回。
8.site_url() 该函数得到你网站的 URL,其中包含了你在 config 文件中设置的 "index" 的值。
$uri uri字符串就是访问路径所带的参数
(1) 如果$uri = '' 返回由base_url和index_page组成的url
(2) 判断$this->item('enable_query_strings')真假,并返回不同形式的地址。(这一项是在application/config/config.php文件中配置的。用来区分传参方式,如果为false就是默认的传参方式example.com/who/what/where/。如果为true就是 example.com/index.php?c=controller&m=function这样的传参方式。)
9.base_url() 该函数返回站点的根 URL,可以在这个函数后拼接一个 URL 路径,用以生成 CSS 或图片文件的 URL。
10._uri_string() 构建uri串让site_url(),base_url()两个函数使用。
11.system_url() 该函数得到 system 文件夹的 URL。
12.set_item() 设置一个配置项
13._assign_to_config() 设置多个配置项(以数组的形式key是要设置的配置项的名字,value 是配置项的值)
源码:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');/** * CodeIgniter * * An open source application development framework for PHP 5.1.6 or newer * * @package CodeIgniter * @author ExpressionEngine Dev Team * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. * @license http://codeigniter.com/user_guide/license.html * @link http://codeigniter.com * @since Version 1.0 * @filesource */// ------------------------------------/** * CodeIgniter Config Class * * This class contains functions that enable config files to be managed * * @package CodeIgniter * @subpackage Libraries * @category Libraries * @author ExpressionEngine Dev Team * @link http://codeigniter.com/user_guide/libraries/config.html */class CI_Config { /** * List of all loaded config values * * @var array */ var $config = array(); /** * List of all loaded config files * * @var array */ var $is_loaded = array(); /** * List of paths to search when trying to load a config file * * @var array */ var $_config_paths = array(APPPATH); /** * Constructor * * Sets the $config data from the primary config.php file as a class variable * * @access public * @param string the config file name * @param boolean if configuration values should be loaded into their own section * @param boolean true if errors should just return false, false if an error message should be displayed * @return boolean if the file was successfully loaded or not */ function __construct() { $this->config =& get_config(); log_message('debug', "Config Class Initialized"); // Set the base_url automatically if none was provided 假如 if ($this->config['base_url'] == '') { if (isset($_SERVER['HTTP_HOST'])) { $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http'; $base_url .= '://'. $_SERVER['HTTP_HOST']; $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); } else { $base_url = 'http://localhost/'; } $this->set_item('base_url', $base_url); } } // -------------------------------- /** * Load Config File * * @access public * @param string the config file name * @param boolean if configuration values should be loaded into their own section * @param boolean true if errors should just return false, false if an error message should be displayed * @return boolean if the file was loaded correctly */ function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE) { $file = ($file == '') ? 'config' : str_replace('.php', '', $file); $found = FALSE; $loaded = FALSE; $check_locations = defined('ENVIRONMENT') ? array(ENVIRONMENT.'/'.$file, $file) : array($file); foreach ($this->_config_paths as $path) { foreach ($check_locations as $location) { $file_path = $path.'config/'.$location.'.php'; if (in_array($file_path, $this->is_loaded, TRUE)) { $loaded = TRUE; continue 2; } if (file_exists($file_path)) { $found = TRUE; break; } } if ($found === FALSE) { continue; } include($file_path); if ( ! isset($config) OR ! is_array($config)) { if ($fail_gracefully === TRUE) { return FALSE; } show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.'); } if ($use_sections === TRUE) { if (isset($this->config[$file])) { $this->config[$file] = array_merge($this->config[$file], $config); } else { $this->config[$file] = $config; } } else { $this->config = array_merge($this->config, $config); } $this->is_loaded[] = $file_path; unset($config); $loaded = TRUE; log_message('debug', 'Config file loaded: '.$file_path); break; } if ($loaded === FALSE) { if ($fail_gracefully === TRUE) { return FALSE; } show_error('The configuration file '.$file.'.php does not exist.'); } return TRUE; } // -------------------------------- /** * Fetch 取得,拿来 a config file item * * * @access public * @param string the config item name * @param string the index name * @param bool * @return string */ function item($item, $index = '') { if ($index == '') { if ( ! isset($this->config[$item])) { return FALSE; } $pref = $this->config[$item]; } else { if ( ! isset($this->config[$index])) { return FALSE; } if ( ! isset($this->config[$index][$item])) { return FALSE; } $pref = $this->config[$index][$item]; } return $pref; } // -------------------------------- /** * Fetch a config file item - adds slash after item (if item is not empty) * * @access public * @param string the config item name * @param bool * @return string */ function slash_item($item) { if ( ! isset($this->config[$item])) { return FALSE; } if( trim($this->config[$item]) == '') { return ''; } return rtrim($this->config[$item], '/').'/'; } // -------------------------------- /** * Site URL * Returns base_url . index_page [. uri_string] * * @access public * @param string the URI string * @return string */ function site_url($uri = '') { if ($uri == '') { return $this->slash_item('base_url').$this->item('index_page'); } if ($this->item('enable_query_strings') == FALSE) { $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix'); return $this->slash_item('base_url').$this->slash_item('index_page').$this->_uri_string($uri).$suffix; } else { return $this->slash_item('base_url').$this->item('index_page').'?'.$this->_uri_string($uri); } } // ------------------------- /** * Base URL * Returns base_url [. uri_string] * * @access public * @param string $uri * @return string */ function base_url($uri = '') { return $this->slash_item('base_url').ltrim($this->_uri_string($uri), '/'); } // ------------------------- /** * Build URI string for use in Config::site_url() and Config::base_url() * * @access protected * @param $uri * @return string */ protected function _uri_string($uri) { if ($this->item('enable_query_strings') == FALSE) { if (is_array($uri)) { $uri = implode('/', $uri); } $uri = trim($uri, '/'); } else { if (is_array($uri)) { $i = 0; $str = ''; foreach ($uri as $key => $val) { $prefix = ($i == 0) ? '' : '&'; $str .= $prefix.$key.'='.$val; $i++; } $uri = $str; } } return $uri; } // -------------------------------- /** * System URL * * @access public * @return string */ function system_url() { $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH)); return $this->slash_item('base_url').end($x).'/'; } // -------------------------------- /** * Set a config file item * * @access public * @param string the config item key * @param string the config item value * @return void */ function set_item($item, $value) { $this->config[$item] = $value; } // -------------------------------- /** * Assign to Config * * This function is called by the front controller (CodeIgniter.php) * after the Config class is instantiated. It permits config items * to be assigned or overriden by variables contained in the index.php file * * @access private * @param array * @return void */ function _assign_to_config($items = array()) { if (is_array($items)) { foreach ($items as $key => $val) { $this->set_item($key, $val); } } }}// END CI_Config class/* End of file Config.php *//* Location: ./system/core/Config.php */

PHP用於構建動態網站,其核心功能包括:1.生成動態內容,通過與數據庫對接實時生成網頁;2.處理用戶交互和表單提交,驗證輸入並響應操作;3.管理會話和用戶認證,提供個性化體驗;4.優化性能和遵循最佳實踐,提升網站效率和安全性。

PHP在數據庫操作和服務器端邏輯處理中使用MySQLi和PDO擴展進行數據庫交互,並通過會話管理等功能處理服務器端邏輯。 1)使用MySQLi或PDO連接數據庫,執行SQL查詢。 2)通過會話管理等功能處理HTTP請求和用戶狀態。 3)使用事務確保數據庫操作的原子性。 4)防止SQL注入,使用異常處理和關閉連接來調試。 5)通過索引和緩存優化性能,編寫可讀性高的代碼並進行錯誤處理。

在PHP中使用預處理語句和PDO可以有效防範SQL注入攻擊。 1)使用PDO連接數據庫並設置錯誤模式。 2)通過prepare方法創建預處理語句,使用佔位符和execute方法傳遞數據。 3)處理查詢結果並確保代碼的安全性和性能。

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP可以輕鬆創建互動網頁內容。 1)通過嵌入HTML動態生成內容,根據用戶輸入或數據庫數據實時展示。 2)處理表單提交並生成動態輸出,確保使用htmlspecialchars防XSS。 3)結合MySQL創建用戶註冊系統,使用password_hash和預處理語句增強安全性。掌握這些技巧將提升Web開發效率。

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 Linux新版
SublimeText3 Linux最新版

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中