This article mainly introduces the cookie operation methods in the CI framework, analyzes three common methods for cookie operation in the CI framework, and analyzes and explains examples of custom extended core controller classes, very useful technology
This article mainly introduces the cookie operation methods in the CI framework, analyzes three common methods for cookie operation in the CI framework, and analyzes and explains examples of custom extended core controller classes, very useful technology
This article describes how to use cookies in the CI framework. Share it with you for your reference. The specific analysis is as follows:
First, set the cookie value using the php original method.
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'];
Second, set the cookie value through the input class library of the CI framework.
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"); // applicable to Controllers
// Echo $ this-> input-> cookie ("username"); // applicable to Controllers
// Echo $ _ COOKIE ['username']; // you can obtain the cookie value in the model class in this way.
// Echo $ _ COOKIE ['Password']; // you can obtain the cookie value in the model class in this way.
Third, set the cookie value through the cookie_helper.php helper function library of the CI framework.
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 extended core controller class
The Code is as follows:
<? Php
Class MY_Controller extends CI_Controller {
// Constructor: In the constructor, you can determine whether the user has logged on. If you log on to the constructor, you can enter the background controller and return to the logon page.
Public function _ construct (){
Parent: :__ construct ();
$ This-> load-> helper ("url ");
$ This-> load-> model ("user_model"); // instantiate an object in the user_model model class
$ This-> cur_user = $ this-> user_model-> is_login (); // checks whether to log on. If yes, the system returns the login user information; otherwise, the system returns false.
If ($ this-> cur_user === false ){
Header ("location:". site_url ("index/login "));
} Else {
// If you have logged on, 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 help you design PHP programs based on the CI framework.