Analysis of cookie operation methods in CI framework

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.