目錄 搜尋
欢迎 目录 快速参考图 基本信息 服务器要求 许可协议 变更记录 关于CodeIgniter 安装 下载 CodeIgniter 安装指导 从老版本升级 疑难解答 介绍 开始 CodeIgniter 是什么? CodeIgniter 速记表 支持特性 应用程序流程图 模型-视图-控制器 架构目标 教程 内容提要 加载静态内容 创建新闻条目 读取新闻条目 结束语 常规主题 CodeIgniter URL 控制器 保留字 视图 模型 辅助函数 使用 CodeIgniter 类库 创建你自己的类库 使用 CodeIgniter 适配器 创建适配器 创建核心系统类 钩子 - 扩展框架的核心 自动装载资源 公共函数 URI 路由 错误处理 缓存 调试应用程序 以CLI方式运行 管理应用程序 处理多环境 PHP替代语法 安全 开发规范 类库参考 基准测试类 日历类 购物车类 配置类 Email 类 加密类 文件上传类 表单验证详解 FTP 类 图像处理类 输入类 Javascript 类 语言类 装载类 迁移类 输出类 分页类 模板解析器类 安全类 Session 类 HTML 表格类 引用通告类 排版类 单元测试类 URI 类 User-Agent 类 表单验证 XML-RPC 和 XML-RPC 服务器 Zip 编码类 缓存适配器 适配器参考 适配器 数据库类 Active Record 类 数据库缓存类 自定义函数调用 数据库配置 连接你的数据库 数据库快速入门例子代码 字段数据 数据库维护类 查询辅助函数 数据库类 查询 生成查询记录集 表数据 事务 数据库工具类 JavaScript类 辅助函数参考 数组辅助函数 CAPTCHA 辅助函数 Cookie Helper 日期辅助函数 目录辅助函数 下载辅助函数 Email 辅助函数 文件辅助函数 表单辅助函数 HTML辅助函数 Inflector 辅助函数 语言辅助函数 数字辅助函数 路径辅助函数 安全辅助函数 表情辅助函数 字符串辅助函数 文本辅助函数 排版辅助函数 URL 辅助函数 XML 辅助函数
文字

CodeIgniter 用户指南 版本 2.1.0

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

输入类

输入类有两个目的:

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

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

安全过滤(Security Filtering)

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

  • If $config['allow_get_array'] is FALSE(default is TRUE), destroys the global GET array.
  • 当 register_globals 被设置为 on 的时候,销毁所有的全局变量。
  • 过滤 GET/POST/COOKIE 数组键,只允许字母-数字(以及一些其它的)字符。
  • 可以过滤跨站脚本攻击 (Cross-site Scripting Hacks) 此功能可全局打开(enabled globally),或者按要求打开。
  • 换行符统一换为 \n(Windows 下为 \r\n)

跨站脚本(XSS)过滤

输入类有能力阻止跨站脚本攻击。如果你想让过滤器遇到 POST 或者 COOKIE 数据时自动运行,你可以通过打开你的 application/config/config.php 文件进行如下设置实现:

$config['global_xss_filtering'] = TRUE;

请参考 安全类 文档以获得更多信息在你的应用中使用跨站脚本过滤。

使用 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()
  • $this->input->cookie()
  • $this->input->server()

$this->input->post()

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

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

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

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

$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()

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

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

如果没有设置参数将返回GET的数组

如果第一参数为NULL,且第二参数为True,则返回经过跨站脚本过滤(XSS Filtering)的数组。

如果没有设从GET中取到数据将返回 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()

这个方法将会搜索POST和GET方式的数据流,首先以POST方式搜索,然后以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->set_cookie()

设置一个 Cookie 的值。这个函数接收两种形式的参数:数组形式和参数形式:

数组形式

用这种形式的话,第一个参数传递的是一个关联数组:

$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);

说明:

只有 name 和 value 是必须的。可以通过将 expire 设置成空来实现删除 Cookie 的操作。

Cookie 的过期时间是以为单位来设置的,他是通过将 Cookie 的存续的时间值加上当前系统时间来得到的。切记,expire 的值仅仅设置为Cookie 需要存续的时间长短,请不要将当前的系统时间加上存续时间后再赋给变量。如果将 expire 设置成零,那么 Cookie 仅在浏览器关闭的时候失效。

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.

参数形式

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()

返回当前用户的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()

返回当前用户正在使用的浏览器的user agent信息。 如果不能得到数据,返回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环境非常有用。返回请求头(header)数组。

$headers = $this->input->request_headers();

$this->input->get_request_header();

返回请求头(request header)数组中某一个元素的值

$this->input->get_request_header('some-header', TRUE);

$this->input->is_ajax_request()

检查服务器头HTTP_X_REQUESTED_WITH是否被设置,并返回布尔值。

$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()

 

翻译贡献者: architectcom, Hex, hk_yuhe, IT不倒翁, soyota, sunjiaxi, xjflyttp, yinzhili, yzheng624, 暗夜星辰
最后修改: 2012-04-27 10:42:28
上一篇: 下一篇: