CI framework to implement a cookie login method detailed, CI framework cookie detailed _php tutorial

Source: Internet
Author: User
Tags setcookie codeigniter smarty template zend framework

CI Framework to implement cookie login method detailed, CI framework cookie detailed


In this paper, we describe the method of the CI framework to realize Cookie landing. Share to everyone for your reference, as follows:

First step: login.php

Login method Public Function login () {//If the user name and password are empty, return to the login page if (Empty ($_post[' username ']) | | empty ($_post[' password ']) {$data [' verifycode '] = rand (1000,9999);//Generate a four-digit verification code//put validation into session, note: parameter is the format of the array $this->session->set_userdata ($   data);   Note: The CI framework default template engine resolves a variable in the template file that does not require a $ sign//$this->parser->parse ("Admin/login", $data);   Smarty Template variable Assignment $this->tp->assign ("Verifycode", $data [' Verifycode ']); The CI framework uses the native PHP syntax output data in the template file//$this->load->view (' login ', $data);//landing page, note: Parameter 2 needs to appear as an array//  Displays the template files set by the Smarty template engine $this->tp->display ("admin/login.php"); }else{$username = isset ($_post[' username ') &&!empty ($_post[' username '])? Trim ($_post[' username ']): ";// User name $password = isset ($_post[' password ')) &&!empty ($_post[' password '])? Trim ($_post[' password ']): ";//Password $ Verifycode = isset ($_post[' Verifycode ')) &&!empty ($_post[' Verifycode '])? Trim ($_post[' Verifycode ']): ";// Verification Code//Verification Code Check if ($verifycode = = $this->session->userdata (' Verifycode '){//According to user name and password to obtain user information, note: Parameter 2 is encrypted password $user _info= $this->user_model->check_user_login ($username, MD5 ($password));     if ($user _info[' user_id ' > 0) {//Put the User ID, username, password into the cookie//the first way to set the cookie: The value of the cookie set by 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 '];     The second way to set cookies: through the CI Framework Input class library $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"),//For controller//echo $this->input->cookie ("username");//For Controller//EC Ho $_cookie[' username '];//in the model class can get the cookie value in this way//echo $_cookie[' password '];//in the model class you can get the cookie value in this way//the third setting Cooki E way: Through the CI framework of the Cookie_helper.php function library file//This way does notis very effective, it is recommended that you take the second way can be//set_cookie ("username", $user _info[' username '],3600);     echo Get_cookie ("username");     Session login using: The user name and user ID into 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 (), the former with index.php, the latter without index.php header ("Location:". Site_url ("Index/index"));   }}else{//Jump to the landing page header ("Location:". Site_url ("Common/login")); }  } }}

Step two: user_model.php

Cookie login: To detect whether the user logged in, if the cookie value is invalid, then return false, if the cookie value is not invalidated, the user name and password in the cookie to obtain user information from the database, if the user information can be obtained, then return the query to the user information, If no user information is queried, returns 0 public function Is_login () {//Gets the value in 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; }//The user information is obtained from the database according to the user name and encryption password, if available, returns the user information obtained, otherwise false, note: Password is encrypted password public function Check_user_login ($username, $  Password) {//here to note: $password for MD5 encrypted password//$this->db->query ("select * from"); Use of shortcut query classes: can provide us with a quick way to obtain data//This array is the query condition//NOTE: Associative array $arr =array (' username ' = = $username,//user name ' password ' = $password  ,//Encryption password ' status ' =>1//account is on);  The data table prefix has been set in the database.php file, so the data table does not need to be prefixed $query = $this->db->get_where ("Users", $arr);  Returns a two-dimensional array//$data = $query->result_array ();  Returns a one-dimensional array $user _info= $query->row_array ();  if (!empty ($user _info)) {return $user _info;  }else{return false; }}

Step Three: Other controllers:

public function __construct () {//Call the parent class's constructor parent::__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 already logged in, reset the cookie's 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 Operation_model model $this->load->model ("Object_model");//Reference Object _model model $this->load->model ("Permission_model");//reference Permission_model model} 

More interested in CodeIgniter related content readers can view this site topic: "CodeIgniter Introductory Tutorial", "CI (codeigniter) Framework Advanced Tutorial", "PHP Excellent Development Framework Summary", "thinkphp Getting Started", " Summary of common methods of thinkphp, "Introduction to Zend Framework Frame", "Introduction to PHP Object-oriented Programming", "Introduction to Php+mysql Database Operation" and "PHP common database Operation Skills Summary"

It is hoped that this article is helpful to the PHP program design based on CodeIgniter framework.

http://www.bkjia.com/PHPjc/1127858.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127858.html techarticle CI Framework to implement the method of cookie landing detailed, CI framework cookie detailed examples of this article describes the CI framework to implement the method of Cookie landing. Share to everyone for your reference, as follows: The first step ...

  • Related Article

    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.