This example describes how cookies are manipulated in the CI framework. Share to everyone for your reference. The specific analysis is as follows:
The first way to set cookies: The value of a cookie that is set by using the native PHP method
Copy Code code 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 value of a cookie through the input class library of the CI framework
Copy Code code 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");//Apply to Controller
echo $this->input->cookie ("username");//Apply to Controller
echo $_cookie[' username '];//can get COOKIE values in this way in the model class
echo $_cookie[' password '];//can get COOKIE values in this way in the model class
the third way to set cookies: To set the value of a cookie through the Cookie_helper.php Helper Library of the CI framework
Copy Code code 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 code as follows:
<?php
Class My_controller extends ci_controller{
Constructor: In the constructor to determine whether the user has landed, if landing, you can enter the background controller, return to jump to the landing 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 log in, if log in, return to login user information, otherwise return false
if ($this->cur_user = = False) {
Header ("Location:". Site_url ("Index/login"));
}else{
If it is already logged in, reset the expiration of the cookie
$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 help you with the PHP program design based on the CI framework.