How to implement cookie login using the CI framework

Source: Internet
Author: User
Tags smarty template
This article mainly introduces how to implement cookie login in the CI framework, and analyzes the steps and related operation skills of the CI framework using cookies in the form of examples, for more information about how to implement cookie login using the CI framework, see the following example. We will share this with you for your reference. The details are as follows:

Step 1: login. php

// Login method public function login () {// if the username and password are empty, return to the login page if (empty ($ _ POST ['username']) | empty ($ _ POST ['password']) {$ data ['verifycode'] = rand ); // Generate a four-digit verification code // put the verification code into the session. note: the parameter format is array $ this-> session-> set_userdata ($ data ); // Note: $ symbol is not required for variables in the template file parsed by the CI framework default template engine. // $ this-> parser-> parse ("admin/login", $ data ); // assign a value to the smarty template variable $ this-> tp-> assign ("verifycode", $ data ['verifycode']); // use the original PHP syntax to input the ci framework in the template file Output data // $ this-> load-> view ('login', $ data); // The logon page. note: parameter 2 needs to display the template file set by the smarty template engine in an array format $ this-> tp-> display ("admin/login. php ");} else {$ username = isset ($ _ POST ['username']) &! Empty ($ _ POST ['username'])? Trim ($ _ POST ['username']): ''; // username $ password = isset ($ _ POST ['password']) &! Empty ($ _ POST ['password'])? Trim ($ _ POST ['password']): ''; // password $ verifycode = isset ($ _ POST ['verifycode']) &! Empty ($ _ POST ['verifycode'])? Trim ($ _ POST ['verifycode']): ''; // verification code // verify the verification code if ($ verifycode = $ this-> session-> userdata ('verifycode') {// obtain user information based on the user name and password, note: parameter 2 is the encrypted password $ user_info = $ this-> user_model-> check_user_login ($ username, md5 ($ password )); if ($ user_info ['User _ id']> 0) {// put the user id, username, and password into the cookie. // The first method to set the cookie is as follows: the cookie value set in the original php method // setcookie ("user_id", $ user_info ['User _ id'], 86500); // setcookie ("username ", $ user_info ['username'], 86500); // setcookie ("password", $ user_info ['password'], 86500 ); // echo $ _ COOKIE ['username']; // method 2 for cookie setting: use the input class library of the CI framework $ this-> input-> set_cookie ("username ", $ user_info ['username'], 3600); $ this-> input-> set_cookie ("password", $ user_info ['password'], 3600 ); $ this-> input-> set_cookie ("user_id", $ user_info ['User _ id'], 3600 ); // echo $ this-> input-> cookie ("password"); // applicable to controller // echo $ this-> input-> cookie ("username "); // applicable to controller // echo $ _ COOKIE ['username']; // you can obtain the cookie value in the model class in this way // echo $ _ COOKIE ['password']; // you can use this method to obtain the cookie value in the model class. // The third method is to set the cookie: Use the cookie_helper.php function library file in the CI framework. // This method is not very good, we recommend that you use the second method: // set_cookie ("username", $ user_info ['username'], 3600); // echo get_cookie ("username "); // used for session login: saves the user name and user ID to the session // $ data ['username'] = $ user_info ['username']; // $ data ['User _ id'] = $ user_info ['User _ id']; // $ this-> session-> set_userdata ($ data ); // jump to the specified page // Note: the difference between site_url () and base_url () is that the former has index. php, which does not contain index. php header ("location :". site_url ("index/index") ;}} else {// jump to the login page header ("location :". site_url ("common/login "));}}}}

Step 2: User_model.php

// Cookie logon: checks whether a user logs on. if the cookie value is invalid, false is returned. if the cookie value is not valid, the user information is obtained from the database based on the user name and password in the cookie, if user information is available, the queried user information is returned. If no user information is found, 0 public function is_login () is returned () {// obtain the value of the cookie if (empty ($ _ COOKIE ['username']) | empty ($ _ COOKIE ['password']) {$ user_info = false;} else {$ user_info = $ this-> check_user_login ($ _ COOKIE ['username'], $ _ COOKIE ['password']);} return $ user_info;} // obtain user information from the database based on the user name and encrypted password. if user information can be obtained, the obtained user information is returned, Otherwise, false is returned. note: The password is the encrypted password. public function check_user_login ($ username, $ password) {// note the following: $ password is the md5 encrypted password // $ this-> db-> query ("select * from"); // use of the quick query class: can provide us with a quick way to get data // This array is a query condition // Note: Associated array $ arr = array ('username' => $ username, // username 'password' => $ password, // encrypted password 'status' => 1 // The account is enabled); // in the database. the prefix of the data table has been set in the PHP file. Therefore, the data table does not need to contain the prefix $ query = $ this-> db-> get_where ("users", $ arr ); // returns a two-dimensional array // $ data = $ query-> r Esult_array (); // returns a one-dimensional array $ user_info = $ query-> row_array (); if (! Empty ($ user_info) {return $ user_info;} else {return false ;}}

Step 3: Other controllers:

Public function _ construct () {// call the constructor parent of the parent class: :__ construct (); $ this-> load-> library ('TP '); // smarty template parsing class $ this-> load-> helper ('URL'); // url function library file $ this-> load-> model ("user_model "); // User_model model class instantiation object $ this-> cur_user = $ this-> user_model-> is_login (); if ($ this-> cur_user = false) {header ("location :". site_url ("common/login");} else {// If you have logged on, reset the cookie validity period $ this-> input-> set_cookie ("username ", $ this-> cur_user ['username'], 3600); $ this-> input-> set_cookie ("password", $ this-> cur_user ['password'], 3600); $ this-> input-> set_cookie ("user_id", $ this-> cur_user ['User _ id'], 3600 );} $ this-> load-> library ('pagination'); // paging class library $ this-> load-> model ("role_model "); // member_model model class $ this-> load-> model ("operation_model"); // reference the operation_model model $ this-> load-> model ("object_model "); // reference the object_model model $ this-> load-> model ("permission_model"); // reference the permission_model model}

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.