Analysis of the operation method of cookies in CI frame _php instance

Source: Internet
Author: User
Tags setcookie

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.

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.