CodeIgniter 用户指南 版本 1.6.3

编辑文档、查看近期更改请 登录注册  找回密码
查看原文

输入类

输入类有两个目的:

  1. 为了安全,预处理输入数据。
  2. 提供helper的一些方法,取得输入数据,并预处理输入数据。

Note: 系统自动加载此类,不用手动加载。

安全过滤(Security Filtering)

当触发一个控制器的时候,安全过滤(Security Filtering)功能自动启动。做以下事情:

跨站脚本过滤(XSS Filtering)

CodeIgniter可以自动过滤POST和COOKIE中的跨站脚本攻击,或者可以在每次处理POST和COOKIE的时候单独使用。缺省情况下,CodeIgniter没有开启自动过滤。因为这样的处理需要一定的cpu资源(a bit of processing overhead)。并且,用户也不一定完全需要。

The XSS filter looks for commonly used techniques to trigger Javascript or other types of code that attempt to hijack cookies or do other malicious things. If anything disallowed is encountered it is rendered safe by converting the data to character entities.

Note: 此功能只应该处理提交的数据。而不应该用于其它情况,因为它要消耗很大的cpu资源。

要想过滤数据中的跨站脚本,使用如下方法:

$this->input->xss_clean()

例子如下:

$data = $this->input->xss_clean($data);

如果你想让codeigniter自动处理POST 或者 COOKIE 数据。你可以在application/config/config.php文件中设定:

$config['global_xss_filtering'] = TRUE;

注意:表单验证类也提供了 XSS 过滤选项。

An optional second parameter, is_image, allows this function to be used to test images for potential XSS attacks, useful for file upload security. When this second parameter is set to TRUE, instead of returning an altered string, the function returns TRUE if the image is safe, and FALSE if it contained potentially malicious information that a browser may attempt to execute.

对于文件上传的安全,此函数的第二个可选参数“is_image",允许此函数检测图像潜在的XSS攻击。当此第二个参数被设置为True的时候,如果图像是安全的,它将会返回True(Bool),而不是被修改后的字符串(String)。如果图像含有浏览器将会执行的潜在恶意信息,函数将会返回False(bool)。

if ($this->input->xss_clean($file, TRUE) === FALSE)
{
    // file failed the XSS test
}

使用 POST, COOKIE, 或 SERVER 数据

CodeIgniter 有3个 helper方法可以让用户取得POST, COOKIE 或 SERVER 的内容。用这些方法比直接使用php方法($_POST['something'])的好处是不用先检查此项目是不是存在。 直接使用php方法,必须先做如下检验:

if ( ! isset($_POST['something']))
{
    $something = FALSE;
}
else
{
    $something = $_POST['something'];
}

用CodeIgniter内建的方法,你可以这样:

$something = $this->input->post('something');

这3个方法是:

$this->input->post()

第一个参数是所要取得的post中的数据:

$this->input->post('some_data');

如果数据不存在,方法将返回 FALSE (布尔值)。

第二个参数是可选的,如果想让取得的数据经过跨站脚本过滤(XSS Filtering),把第二个参数设为TRUE

$this->input->post('some_data', TRUE);

$this->input->get()

This function is identical to the post function, only it fetches get data:

此方法类似post方法,用来取得get数据:

$this->input->get('some_data', TRUE);

$this->input->get_post()

This function will search through both the post and get streams for data, looking first in post, and then in get:

$this->input->get_post('some_data', TRUE);

$this->input->cookie()

此方法类似post方法,用来取得cookie数据:

$this->input->cookie('some_data', TRUE);

$this->input->server()

此方法类似上面两个方法,用来取得server数据:

$this->input->server('some_data');

$this->input->ip_address()

返回当前用户的IP。如果IP地址无效,返回0.0.0.0的IP:

echo $this->input->ip_address();

$this->input->valid_ip($ip)

测试输入的IP地址是不是有效,返回布尔值TRUE或者FALSE。 注意:$this->input->ip_address()自动测试输入的IP地址本身格式是不是有效。

if ( ! $this->input->valid_ip($ip))
{
     echo 'Not Valid';
}
else
{
     echo 'Valid';
}

$this->input->user_agent()

返回当前用户的浏览器 (web browser) 如果不能得到数据,返回FALSE。

echo $this->input->user_agent();

 

翻译贡献者: architectcom, Hex, xjflyttp
最后修改: 2008-08-05 21:36:34