非常教程

Codeigniter 3参考手册

输入 | Input

输入类 | Input Class

输入类有两个目的:

  1. 它预处理全局输入数据以确保安全。
  2. 它提供了一些辅助方法来获取输入数据并对其进行预处理。

注意

该类由系统自动初始化,因此不需要手动执行。

  • 输入过滤
    • 安全筛选
    • XSS过滤
  • 访问表单数据
    • 使用POST,GET,COOKIE或SERVER数据
    • 使用php://输入流
  • 类参考

输入过滤

安全筛选

当调用新的控制器时,将自动调用安全过滤方法。它执行以下操作:

  • 如果$config['allow_get_array']为FALSE(默认值为TRUE),则会销毁全局GET数组。
  • 销毁register_globals处于打开状态的所有全局变量。
  • 过滤GET / POST / COOKIE数组键,仅允许字母数字(和其他几个字符)。
  • 提供XSS(跨站脚本攻击)过滤。这可以在全球启用,或根据要求启用。
  • 将换行符标准化为PHP_EOL(在基于UNIX的操作系统中为\ n,在Windows下为\ r \ n)。这是可配置的。

XSS过滤

Input类可以自动过滤输入以防止跨站脚本攻击。如果您希望筛选器在每次遇到POST或COOKIE数据时自动运行,您可以通过打开application / config / config.php文件并设置它来启用它:

$config['global_xss_filtering'] = TRUE;

有关在您的应用程序中使用XSS Filtering的信息,请参阅Security类文档。

重要

'global_xss_filtering'设置为DEPRECATED,仅用于向后兼容的目的。应该在输出上执行XSS转义,而不是输入

访问表单数据

使用POST,GET,COOKIE或SERVER数据

CodeIgniter带有助手方法,可以让你获取POST,GET,COOKIE或SERVER项目。使用提供的方法而不是直接获取项目的主要优点$_POST['something']是方法将检查项目是否设置,如果不是,则返回NULL。这使您可以方便地使用数据,而不必先测试项目是否存在。换句话说,通常你可能会这样做:

$something = isset($_POST['something']) ? $_POST['something'] : NULL;

使用CodeIgniter的内置方法,您可以简单地执行此操作:

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

主要方法是:

  • $this->input->post()
  • $this->input->get()
  • $this->input->cookie()
  • $this->input->server()

Using the php://input stream

如果你想利用PUT,DELETE,PATCH或其他奇特的请求方法,它们只能通过一个特殊的输入流来访问,它只能被读取一次。这不像从$_POST数组读取那样简单,因为它会一直存在,并且您可以尝试访问多个变量,而不必关心在所有POST数据中只能有一个镜头。

CodeIgniter会为你处理这个问题,你可以随时从php://输入流中读取数据,只需使用$raw_input_stream属性:

$this->input->raw_input_stream;

另外,如果输入流像$ _POST一样进行表单编码,则可以通过调用input_stream()方法来访问其值:

$this->input->input_stream('key');

类似于其他方法,如get()post(),如果未找到所请求的数据,它会返回NULL,你也可以决定是否通过运行数据xss_clean()通过传递一个布尔值作为第二个参数:

$this->input->input_stream('key', TRUE); // XSS Clean
$this->input->input_stream('key', FALSE); // No XSS filter

注意

您可以利用method()以了解您是否正在读取PUT,DELETE或PATCH数据。

类参考

class CI_Input$raw_input_stream

只读属性将返回php://输入数据原样。

该属性可以多次读取。

post([$index = NULL[, $xss_clean = NULL]])

参数:

$ index(mixed) - POST参数名称$ xss_clean(bool) - 是否应用XSS过滤

返回:

$ _POST如果没有提供参数,否则POST值如果找到,否则返回NULL

返回类型:

  • $ indexmixed) - POST参数名称
  • $ xss_cleanbool) - 是否应用XSS过滤
Returns:  $\_POST if no parameters supplied, otherwise the POST value if found or NULL if not
Return type:  mixed
第一个参数将包含您正在查找的POST项目的名称:

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

如果您尝试检索的项目不存在,则该方法返回NULL。

第二个可选参数允许您通过XSS过滤器运行数据。通过将第二个参数设置为布尔TRUE或将其设置$config['global_xss_filtering']为TRUE 来启用它。

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

要返回一个没有任何参数的所有POST项目调用的数组。

要返回所有POST项目并将它们传递给XSS筛选器,请将第一个参数设置为NULL,同时将第二个参数设置为布尔值TRUE。

$this->input->post(NULL, TRUE); // returns all POST items with XSS filter $this->input->post(NULL, FALSE); // returns all POST items without XSS filter

要返回多个POST参数的数组,请将所有必需的键作为数组传递。

$this->input->post(array('field1', 'field2'));

在这里应用相同的规则,为了检索启用了XSS过滤的参数,将第二个参数设置为布尔TRUE。

$this->input->post(array('field1', 'field2'), TRUE);

get([$index = NULL[, $xss_clean = NULL]])

参数:

$ index(mixed) - GET参数名称$ xss_clean(bool) - 是否应用XSS过滤

返回:

$ _GET如果没有提供参数,否则GET值如果找到,否则返回NULL

返回类型:

  • $ indexmixed) - GET参数名称
  • $ xss_cleanbool) - 是否应用XSS过滤
Returns:  $\_GET if no parameters supplied, otherwise the GET value if found or NULL if not
Return type:  mixed
This method is identical to `post()`, only it fetches GET data.

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

不带任何参数返回所有GET项目数组的调用。

要返回所有GET项并将它们传递给XSS筛选器,请将第一个参数设置为NULL,同时将第二个参数设置为布尔值TRUE。

$ this-> input-> get(NULL,TRUE); //返回带有XSS过滤器的所有GET项目$ this-> input-> get(NULL,FALSE); //返回所有没有XSS过滤的GET项目

要返回多个GET参数的数组,请将所有必需的键作为数组传递。

$this->input->get(array('field1', 'field2'));

在这里应用相同的规则,为了检索启用了XSS过滤的参数,将第二个参数设置为布尔TRUE。

$this->input->get(array('field1', 'field2'), TRUE);

post_get($index[, $xss_clean = NULL])

参数:

$ index(string) - POST / GET参数名称$ xss_clean(bool) - 是否应用XSS过滤

返回:

如果找到POST / GET值,则返回NULL

返回类型:

  • $ indexstring) - POST / GET参数名称
  • $ xss_cleanbool) - 是否应用XSS过滤
Returns:  POST/GET value if found, NULL if not
Return type:  mixed
This method works pretty much the same way as `post()` and `get()`, only combined. It will search through both POST and GET streams for data, looking in POST first, and then in GET:

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

get_post($index[, $xss_clean = NULL])

参数:

$ index(string) - GET / POST参数名称$ xss_clean(bool) - 是否应用XSS过滤

返回:

如果找到GET / POST值,则返回NULL

返回类型:

  • $ indexstring) - GET / POST参数名称
  • $ xss_cleanbool) - 是否应用XSS过滤
Returns:  GET/POST value if found, NULL if not
Return type:  mixed
This method works the same way as `post_get()` only it looks for GET data first.

$this->input->get_post(‘some_data’, TRUE); Note

此方法用于post_get()表现得很像,但它的行为在CodeIgniter 3.0中发生了变化。

cookie([$index = NULL[, $xss_clean = NULL]])

参数:

$ index(混合) - COOKIE名称$ xss_clean(bool) - 是否应用XSS过滤

返回:

如果没有提供参数,则返回$ _COOKIE,否则返回COOKIE值,否则返回NULL

返回类型:

  • $ index混合) - COOKIE名称
  • $ xss_cleanbool) - 是否应用XSS过滤
Returns:  $\_COOKIE if no parameters supplied, otherwise the COOKIE value if found or NULL if not
Return type:  mixed
This method is identical to `post()` and `get()`, only it fetches cookie data:

$this->input->cookie('some_cookie'); $this->input->cookie('some_cookie, TRUE); // with XSS filter

要返回多个Cookie值的数组,请将所有必需的键作为数组传递。

$this->input->cookie(array('some_cookie', 'some_cookie2'));

注意

与Cookie帮助器函数不同get_cookie(),此方法不会预先配置您的配置$config['cookie_prefix']值。

server($index[, $xss_clean = NULL])

参数:

$ index(mixed) - 值名称$ xss_clean(bool) - 是否应用XSS过滤

返回:

如果找到$ _SERVER项目值,则返回NULL

返回类型:

  • $ index混合) - 值名称
  • $ xss_cleanbool) - 是否应用XSS过滤
Returns:  $\_SERVER item value if found, NULL if not
Return type:  mixed
This method is identical to the `post()`, `get()` and `cookie()` methods, only it fetches server data (`$_SERVER`):

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

要返回多个$_SERVER值的数组,请将所有必需的键作为数组传递。

$this->input->server(array('SERVER_PROTOCOL', 'REQUEST_URI'));

input_stream([$index = NULL[, $xss_clean = NULL]])

参数:

$ index(mixed) - 键名$ xss_clean(bool) - 是否应用XSS过滤

返回:

输入流数组,如果没有提供参数,否则指定的值如果找到,否则返回NULL

返回类型:

  • $ index混合) - 密钥名称
  • $ xss_cleanbool) - 是否应用XSS过滤
Returns:  Input stream array if no parameters supplied, otherwise the specified value if found or NULL if not
Return type:  mixed
This method is identical to `get()`, `post()` and `cookie()`, only it fetches the _php://input_ stream data.

set_cookie($name = ''[, $value = ''[, $expire = ''[, $domain = ''[, $path = '/'[, $prefix = ''[, $secure = NULL[, $httponly = NULL]]]]]]])

参数:

$ name(字符串) - Cookie值$ expire(int) - Cookie过期时间(以秒为单位)$ domain(字符串) - Cookie域$ path(字符串) - Cookie路径$ prefix (字符串) - Cookie名称前缀$ secure(bool) - 是否仅通过HTTPS传输cookie $ httponly(bool) - 是否仅允许HTTP请求访问Cookie(无JavaScript)

返回类型:

void

  • $ name混合) - Cookie名称或参数数组
  • $ value字符串) - Cookie值
  • $ expireint) - 以秒为单位的Cookie过期时间
  • $域字符串) - Cookie域
  • $ pathstring) - Cookie路径
  • $ prefix字符串) - Cookie名称前缀
  • $ securebool) - 是否仅通过HTTPS传输cookie
  • $ httponlybool) - 是否仅允许HTTP请求访问cookie(无JavaScript)
Return type:  void
Sets a cookie containing the values you specify. There are two ways to pass information to this method so that a cookie can be set: Array Method, and Discrete Parameters:

数组方法

使用这种方法,将关联数组传递给第一个参数:

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

笔记

只有名字和价值是必需的。要删除cookie,将其设置为过期空白。

到期时间以为单位设置,并将添加到当前时间。不要包含时间,而只需要从现在起您希望cookie有效的秒数。如果过期设置为零,则只有在浏览器处于打开状态时,cookie才会持续。

无论网站的请求方式如何,对于站点级的Cookie,请将您的网址添加到以句点开头的网,如下所示:.your-domain.com

由于该方法设置了根路径,因此通常不需要该路径。

只有当您需要避免与服务器上其他名称相同的cookie发生名称冲突时,才需要前缀。

仅Http安全标志,省略时,将默认为您$config['cookie_httponly']$config['cookie_secure']设置。

离散参数

如果您愿意,可以通过使用各个参数传递数据来设置Cookie:

$ this-> input-> set_cookie($ name,$ value,$ expire,$ domain,$ path,$ prefix,$ secure);

ip_address()

返回:

访问者的IP地址或“0.0.0.0”,如果无效

返回类型:

valid_ip($ip[, $which = ''])

参数:

$ ip(string) - IP地址$ which(string) - IP协议('ipv4'或'ipv6')

返回:

如果地址有效则为TRUE,否则为FALSE

返回类型:

布尔

  • $ ip字符串) - IP地址
  • $ whichstring) - IP协议('ipv4'或'ipv6')
Returns:  TRUE if the address is valid, FALSE if not
Return type:  bool
Takes an IP address as input and returns TRUE or FALSE (boolean) depending on whether it is valid or not.

注意

上面的$ this-> input-> ip_address()方法自动验证IP地址。

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

接受可选的第二个字符串参数'ipv4'或'ipv6'来指定IP格式。这两种格式的默认检查。

user_agent([$xss_clean = NULL])

返回:

用户代理字符串,如果未设置,则为NULL

参数:

$ xss_clean(bool) - 是否应用XSS过滤

返回类型:

  • $ xss_cleanbool) - 是否应用XSS过滤
Return type:  mixed
Returns the user agent string (web browser) being used by the current user, or NULL if it’s not available.

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

有关从用户代理字符串提取信息的方法,请参阅用户代理类。

request_headers([$xss_clean = FALSE])

参数:

$ xss_clean(bool) - 是否应用XSS过滤

返回:

一组HTTP请求标头

返回类型:

排列

  • $ xss_cleanbool) - 是否应用XSS过滤
Returns:  An array of HTTP request headers
Return type:  array
Returns an array of HTTP request headers. Useful if running in a non-Apache environment where [apache\_request\_headers()](https://php.net/apache_request_headers) will not be supported.

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

get_request_header($index[, $xss_clean = FALSE])

参数:

$ index(string) - HTTP请求头名称$ xss_clean(bool) - 是否应用XSS过滤

返回:

HTTP请求标头或NULL,如果未找到

返回类型:

  • $ indexstring) - HTTP请求标题名称
  • $ xss_cleanbool) - 是否应用XSS过滤
返回:HTTP请求标头或NULL,如果未找到
Return type:  string
Returns a single member of the request headers array or NULL if the searched header is not found.

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

is_ajax_request()

返回:

如果它是Ajax请求则为TRUE,否则为FALSE

返回类型:

布尔

is_cli_request()

返回:

如果是CLI请求则为TRUE,否则为FALSE

返回类型:

布尔

method([$upper = FALSE])

参数:

$ upper(bool) - 是否以大写或小写形式返回请求方法名称

返回:

HTTP请求方法

返回类型:

  • $ upperbool) - 是否以大写或小写形式返回请求方法名称
Returns:  HTTP request method
Return type:  string
Returns the `$_SERVER['REQUEST_METHOD']`, with the option to set it in uppercase or lowercase.

echo $this->input->method(TRUE); // Outputs: POST echo $this->input->method(FALSE); // Outputs: post echo $this->input->method(); // Outputs: post

输入 | Input相关

Codeigniter 3

CodeIgniter 是一个PHP MVC框架,特点是超轻量级、有数据加密、有灵活URI路由等。对于 PHP 程序员来说,它小巧但功能强大。

主页 https://codeigniter.com/
源码 https://github.com/bcit-ci/CodeIgniter
版本 3
发布版本 3.1.5