輸入類(lèi)
輸入類(lèi)有兩個(gè)目的:
- 為了安全,預(yù)處理輸入數(shù)據(jù)。
- 提供helper的一些方法,取得輸入數(shù)據(jù),并預(yù)處理輸入數(shù)據(jù)。
說(shuō)明: 系統(tǒng)自動(dòng)加載此類(lèi),不用手動(dòng)加載。
安全過(guò)濾(Security Filtering)
當(dāng)觸發(fā)一個(gè)控制器的時(shí)候,安全過(guò)濾(Security Filtering)功能自動(dòng)啟動(dòng)。做以下事情:
- If $config['allow_get_array'] is FALSE(default is TRUE), destroys the global GET array.
- 當(dāng) register_globals 被設(shè)置為 on 的時(shí)候,銷(xiāo)毀所有的全局變量。
- 過(guò)濾 GET/POST/COOKIE 數(shù)組鍵,只允許字母-數(shù)字(以及一些其它的)字符。
- 可以過(guò)濾跨站腳本攻擊 (Cross-site Scripting Hacks) 此功能可全局打開(kāi)(enabled globally),或者按要求打開(kāi)。
- 換行符統(tǒng)一換為 \n(Windows 下為 \r\n)
跨站腳本(XSS)過(guò)濾
輸入類(lèi)有能力阻止跨站腳本攻擊。如果你想讓過(guò)濾器遇到 POST 或者 COOKIE 數(shù)據(jù)時(shí)自動(dòng)運(yùn)行,你可以通過(guò)打開(kāi)你的 application/config/config.php 文件進(jìn)行如下設(shè)置實(shí)現(xiàn):
$config['global_xss_filtering'] = TRUE;
請(qǐng)參考 安全類(lèi) 文檔以獲得更多信息在你的應(yīng)用中使用跨站腳本過(guò)濾。
使用 POST, COOKIE, 或 SERVER 數(shù)據(jù)
CodeIgniter 有3個(gè) helper方法可以讓用戶(hù)取得POST, COOKIE 或 SERVER 的內(nèi)容。用這些方法比直接使用php方法($_POST['something'])的好處是不用先檢查此項(xiàng)目是不是存在。 直接使用php方法,必須先做如下檢驗(yàn):
if ( ! isset($_POST['something']))
{
????$something = FALSE;
}
else
{
????$something = $_POST['something'];
}
用CodeIgniter內(nèi)建的方法,你可以這樣:
$something = $this->input->post('something');
這3個(gè)方法是:
- $this->input->post()
- $this->input->cookie()
- $this->input->server()
$this->input->post()
第一個(gè)參數(shù)是所要取得的post中的數(shù)據(jù):
$this->input->post('some_data');
如果數(shù)據(jù)不存在,方法將返回 FALSE (布爾值)。
第二個(gè)參數(shù)是可選的,如果想讓取得的數(shù)據(jù)經(jīng)過(guò)跨站腳本過(guò)濾(XSS Filtering),把第二個(gè)參數(shù)設(shè)為T(mén)RUE。
$this->input->post('some_data', TRUE);
To return an array of all POST items call without any parameters.
To return all POST items and pass them through the XSS filter set the first parameter NULL while setting the second parameter to boolean;
The function returns FALSE (boolean) if there are no items in the POST.
$this->input->post(NULL, TRUE); // returns all POST items with XSS filter
$this->input->post(); // returns all POST items without XSS filter
$this->input->get()
此方法類(lèi)似post方法,用來(lái)取得get數(shù)據(jù):
$this->input->get('some_data', TRUE);
如果沒(méi)有設(shè)置參數(shù)將返回GET的數(shù)組
如果第一參數(shù)為NULL,且第二參數(shù)為T(mén)rue,則返回經(jīng)過(guò)跨站腳本過(guò)濾(XSS Filtering)的數(shù)組。
如果沒(méi)有設(shè)從GET中取到數(shù)據(jù)將返回 FALSE (boolean)
$this->input->get(NULL, TRUE); // returns all GET items with XSS filter
$this->input->get(); // returns all GET items without XSS filtering
$this->input->get_post()
這個(gè)方法將會(huì)搜索POST和GET方式的數(shù)據(jù)流,首先以POST方式搜索,然后以GET方式搜索:
$this->input->get_post('some_data', TRUE);
$this->input->cookie()
此方法類(lèi)似post方法,用來(lái)取得cookie數(shù)據(jù):
$this->input->cookie('some_data', TRUE);
$this->input->server()
此方法類(lèi)似上面兩個(gè)方法,用來(lái)取得server數(shù)據(jù):
$this->input->server('some_data');
$this->input->set_cookie()
設(shè)置一個(gè) Cookie 的值。這個(gè)函數(shù)接收兩種形式的參數(shù):數(shù)組形式和參數(shù)形式:
數(shù)組形式
用這種形式的話,第一個(gè)參數(shù)傳遞的是一個(gè)關(guān)聯(lián)數(shù)組:
$cookie = array(
????'name'???=> 'The Cookie Name',
????'value'??=> 'The Value',
????'expire' => '86500',
????'domain' => '.some-domain.com',
????'path'???=> '/',
????'prefix' => 'myprefix_',
????'secure' => TRUE
);
$this->input->set_cookie($cookie);
說(shuō)明:
只有 name 和 value 是必須的??梢酝ㄟ^(guò)將 expire 設(shè)置成空來(lái)實(shí)現(xiàn)刪除 Cookie 的操作。
Cookie 的過(guò)期時(shí)間是以秒為單位來(lái)設(shè)置的,他是通過(guò)將 Cookie 的存續(xù)的時(shí)間值加上當(dāng)前系統(tǒng)時(shí)間來(lái)得到的。切記,expire 的值僅僅設(shè)置為Cookie 需要存續(xù)的時(shí)間長(zhǎng)短,請(qǐng)不要將當(dāng)前的系統(tǒng)時(shí)間加上存續(xù)時(shí)間后再賦給變量。如果將 expire 設(shè)置成零,那么 Cookie 僅在瀏覽器關(guān)閉的時(shí)候失效。
For site-wide cookies regardless of how your site is requested, add your URL to the domain starting with a period, like this: .your-domain.com
The path is usually not needed since the function sets a root path.
The prefix is only needed if you need to avoid name collisions with other identically named cookies for your server.
The secure boolean is only needed if you want to make it a secure cookie by setting it to TRUE.
參數(shù)形式
If you prefer, you can set the cookie by passing data using individual parameters:
$this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);
$this->input->ip_address()
返回當(dāng)前用戶(hù)的IP。如果IP地址無(wú)效,返回0.0.0.0的IP:
echo $this->input->ip_address();
$this->input->valid_ip($ip)
測(cè)試輸入的IP地址是不是有效,返回布爾值TRUE或者FALSE。 注意:$this->input->ip_address()自動(dòng)測(cè)試輸入的IP地址本身格式是不是有效。
if ( ! $this->input->valid_ip($ip))
{
???? echo 'Not Valid';
}
else
{
???? echo 'Valid';
}
$this->input->user_agent()
返回當(dāng)前用戶(hù)正在使用的瀏覽器的user agent信息。 如果不能得到數(shù)據(jù),返回FALSE。
echo $this->input->user_agent();
See the User Agent Class for methods which extract information from the user agent string.
$this->input->request_headers()
在不支持apache_request_headers()的非Apache環(huán)境非常有用。返回請(qǐng)求頭(header)數(shù)組。
$headers = $this->input->request_headers();
$this->input->get_request_header();
返回請(qǐng)求頭(request header)數(shù)組中某一個(gè)元素的值
$this->input->get_request_header('some-header', TRUE);
$this->input->is_ajax_request()
檢查服務(wù)器頭HTTP_X_REQUESTED_WITH是否被設(shè)置,并返回布爾值。
$this->input->is_ajax_request()
$this->input->is_cli_request()
Checks to see if the STDIN constant is set, which is a failsafe way to see if PHP is being run on the command line.
$this->input->is_cli_request()
?