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);
说明:
只有 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.
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()
让你获取一个cookie。第一个参数包含你要查找的cookie名(包含前缀):
get_cookie('some_cookie');
如果你试图要检索的项目不存在,本方法则返回FALSE(布尔型)。
第二个可选参数能让你使用通过XSS过滤器获得的数据。只要把第二个参数设置成布尔值的TRUE就能使用;
get_cookie('some_cookie', TRUE);
delete_cookie()
可以让你删除一个cookie,除非你设置了一个自定义的路径或者其他变量,否则只要给出cookie的名字就行了:
delete_cookie("name");
这个函数与set_cookie()函数比较像, 除了它没有值和超时参数. 你可以把一个数组作为第一个参数提交,也可以像下面这样,设置独立的参数.
delete_cookie($name, $domain, $path, $prefix)