状态 草稿
Todo document xss_clean
官方最后更新时间 2008/08/11 05:59

输入库(Input Library)

输入库对于两件事情非常有用:

  1. 安全性的全局输入预处理
  2. 提供一些有用的功能来检索输入数据

注意

  • $_REQUEST 和 $_GLOBAL 变量在 Kohana 中不可用。
  • $_POST,$_GET,$_COOKIE 和 $_SERVER 均转换为 utf-8。
  • 当输入库加载后处理全局的 GET, POST and COOKIE 数据。

加载库

此库为自动加载所以不需要自己加载它。使用 $this→input 进行操作。

方法

get()

allows you to retrieve GET variables. if global XSS filtering is on (config) then data returned by this function will be filtered.

  • [string] variable to retrieve – default = empty array (returns all variables)
//URL is http://www.example.com/index.php?articleId=123&file=text.txt
//Note that print statements are for documentation purpose only
 
print Kohana::debug($this->input->get());
print Kohana::debug($this->input->get('file'));

It will result in HTML as:

Array
(
    [articleId] => 123
    [file] => text.txt
)
 
text.txt

You can also pass a default value and manually XSS clean the request by passing parameters.

$this->input->get('file','default_value'); //'default_value' is the default value if the key doesn't exist.
$this->input->get('file',null,true); //manually apply XSS clean

post()

allows you to retrieve POST variables. if global XSS filtering is on (config) then data returned by this function will be filtered.

  • [string] variable to retrieve – default = empty array (returns all variables)
//POST variables are articleId=123 and file=text.txt
//Note that print statements are for documentation purpose only
 
print Kohana::debug($this->input->post());
print Kohana::debug($this->input->post('file'));

It will result in HTML as:

Array
(
    [articleId] => 123
    [file] => text.txt
)
 
text.txt

You can also pass a default value and manually XSS clean the request by passing parameters.

$this->input->post('file','default_value'); //'default_value' is the default value if the key doesn't exist.
$this->input->post('file',null,true); //manually apply XSS clean

cookie()

allows you to retrieve COOKIE variables. if global XSS filtering is on (config) then data returned by this function will be filtered.

  • [string] variable to retrieve – default = empty array (returns all variables)
//COOKIE name is "username" and the contents of this cookie is "aart-jan".
//Note that print statements are for documentation purpose only
 
print Kohana::debug($this->input->cookie());
print Kohana::debug($this->input->cookie('username'));

It will result in HTML as:

Array
(
    [username] => aart-jan
)
aart-jan

You can also pass a default value and manually XSS clean the request by passing parameters.

$this->input->cookie('username','default_value'); //'default_value' is the default value if the key doesn't exist.
$this->input->cookie('username',null,true); //manually apply XSS clean

server()

allows you to retrieve SERVER variables. if global XSS filtering is on (config) then data returned by this function will be filtered. An overview of these variables can be found in the php documentation

  • [string] variable to retrieve – default = empty array (returns all variables)
//Note that print statements are for documentation purpose only
print Kohana::debug($this->input->server('HTTP_HOST')); //this example ran on a local server

It will result in HTML as:

localhost

You can also pass a default value and manually XSS clean the request by passing parameters.

$this->input->server('HTTP_HOST','default_value'); //'default_value' is the default value if the key doesn't exist.
$this->input->server('HTTP_HOST',null,true); //manually apply XSS clean

ip_address()

'ip_address' returns the IP-address of the user visiting using your webapplication. It returns '0.0.0.0' if there's no IP.

print $this->input->ip_address(); //this example ran on a local server

It will result in HTML as:

127.0.0.1

valid_ip()

'valid_ip' will check whether the specified IP is a valid IPV4 ip-address according to the RFC specifications.

  • [string] IP address to be validated

This function is identical to the IP address validation helper.

xss_clean()

allows you to clean a string to make sure it is safe.

  • [string/array] The string or the array of strings to clean
echo $this->input->xss_clean($suspect_input);
libraries/input.txt · 最后更改: 2008/10/24 04:21 由 icyleaf