CodeIgniter 用户指南 版本 1.6.3

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

Cookie Helper

Cookie Helper 中包含了多个cookie辅助函数.

装载这个 helper

这个 helper 可以通过下面的方法来装载:

$this->load->helper('cookie');

下面这种方法同样可以:

set_cookie()

首先指定一个容器来存放你需要写入 cookie 的指定的值. 可以使用数组或参数的方式来将指定的值存为 cookie.

数组方式

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

$cookie = array(
                   'name'   => 'The Cookie Name',
                   'value'  => 'The Value',
                   'expire' => '86500',
                   'domain' => '.some-domain.com',
                   'path'   => '/',
                   'prefix' => 'myprefix_',
               );

set_cookie($cookie);

说明:

只需要变量名和变量值。

Cookie的过期时间是以为单位来设置的, 他是通过将Cookie的存续的时间值加上当前系统时间来得到的。切记,expire的值仅仅设置为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.

Discrete Parameters

If you prefer, you can set the cookie by passing data using individual parameters:

set_cookie($name, $value, $expire, $domain, $path, $prefix);

get_cookie()

Lets you fetch a cookie. The first parameter will contain the name of the cookie you are looking for (including any prefixes):

get_cookie('some_cookie');

The function returns FALSE (boolean) if the item you are attempting to retrieve does not exist.

The second optional parameter lets you run the data through the XSS filter. It's enabled by setting the second parameter to boolean TRUE;

get_cookie('some_cookie', TRUE);

delete_cookie()

Lets you delete a cookie. Unless you've set a custom path or other values, only the name of the cookie is needed:

delete_cookie("name");

This function is otherwise identical to set_cookie(), except that it does not have the value and expiration parameters. You can submit an array of values in the first parameter or you can set discrete parameters.

delete_cookie($name, $domain, $path, $prefix)

 

翻译贡献者: 498621, Hex, pipi95
最后修改: 2009-01-08 20:58:21