Home >Backend Development >PHP Tutorial >CodeIgniter中使用cookie的三种方式详解_PHP

CodeIgniter中使用cookie的三种方式详解_PHP

WBOY
WBOYOriginal
2016-06-01 11:50:11880browse

CI框架

cookie在php程序设计中应用十分广泛,本文所述CodeIgniter中使用cookie主要有以下三种方式,读者可以根据自身项目需求酌情采用。

第一种方式:采用php原生态的方法设置的cookie的值

setcookie("user_id",$user_info['user_id'],86500);
setcookie("username",$user_info['username'],86500);
setcookie("password",$user_info['password'],86500);
//echo$_COOKIE['username'];

第二种方式:通过CI框架的input类库设置cookie的值

$this->input->set_cookie("username",$user_info['username'],60);
$this->input->set_cookie("password",$user_info['password'],60);
$this->input->set_cookie("user_id",$user_info['user_id'],60);
//echo$this->input->cookie("password");//适用于控制器
//echo$this->input->cookie("username");//适用于控制器
//echo$_COOKIE['username'];//在模型类中可以通过这种方式获取cookie值
//echo$_COOKIE['password'];//在模型类中可以通过这种方式获取cookie值

第三种方式:通过CI框架的cookie_helper.php辅助函数库设置cookie的值

set_cookie("username",$user_info['username'],60);
set_cookie("password",$user_info['password'],60);
set_cookie("user_id",$user_info['user_id'],60);
//echoget_cookie("username");

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn