Home >Backend Development >PHP Tutorial >Analysis of cookie operation methods in CI framework, cicookie_PHP tutorial

Analysis of cookie operation methods in CI framework, cicookie_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:11:47885browse

Analysis of the operation method of cookies in the CI framework, cicookie

The example in this article describes how to operate cookies in the CI framework. Share it with everyone for your reference. The specific analysis is as follows:

The first way to set cookies: use the original PHP method to set the cookie value

Copy code The code is as follows:
setcookie("user_id",$user_info['user_id'],86500);
setcookie("username",$user_info['username'],86500);
setcookie("password",$user_info['password'],86500);
//echo $_COOKIE['username'];


The second way to set cookies: set the cookie value through the input class library of the CI framework
Copy code The code is as follows:
$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");//Applies to controller
//echo $this->input->cookie("username");//Applies to controller
//echo $_COOKIE['username'];//The cookie value can be obtained in this way in the model class
//echo $_COOKIE['password'];//The cookie value can be obtained in this way in the model class


The third way to set cookies: Set the cookie value through the cookie_helper.php auxiliary function library of the CI framework
Copy code The code is as follows:
set_cookie("username",$user_info['username'],60);
set_cookie("password",$user_info['password'],60);
set_cookie("user_id",$user_info['user_id'],60);
//echo get_cookie("username");

Example custom extension core controller class

Copy code The code is as follows:
class MY_Controller extends CI_Controller{

//Constructor: Determine whether the user has logged in in the constructor. If logged in, you can enter the background controller and return to the login page
Public function __construct(){
         parent::__construct(); 
           $this->load->helper("url");
             $this->load->model("user_model");//user_model model class instantiation object
            $this->cur_user=$this->user_model->is_login();//Check whether you are logged in, if logged in, return the logged in user information, otherwise return false
If($this->cur_user === false){
header("location:".site_url("index/login"));
          }else{ 
//If you are already logged in, reset the cookie validity period
                   $this->input->set_cookie("username",$this->cur_user['username'],60);
                    $this->input->set_cookie("password",$this->cur_user['password'],00);
                  $this->input->set_cookie("user_id",$this->cur_user['user_id'],60);
                                                                                                              }  
}
?>
I hope this article will be helpful to everyone’s PHP programming based on the CI framework.

http://www.bkjia.com/PHPjc/926874.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/926874.htmlTechArticleAnalysis of the operation method of cookies in the CI framework, cicookie This article describes the operation method of cookies in the CI framework. Share it with everyone for your reference. The specific analysis is as follows: The first setting cook...
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