CI Framework Implementation Cookie Login method detailed _php Example

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

This article illustrates the method of the CI Framework implementation cookie login. Share to everyone for your reference, specific as follows:

First step: login.php

Login method Public Function login () {//if user name and password is empty, return to landing page if (Empty ($_post[' username ')) | | empty ($_post[' password '])) { $data [' verifycode '] = rand (1000,9999);//Generate a four-digit CAPTCHA//validate the code into session, note: The parameter is the format of the array $this->session->set_
   UserData ($data);
   Note: The CI framework default template engine resolves template files in which variables do not need $ symbol//$this->parser->parse ("Admin/login", $data);
   Smarty Template variable Assignment $this->tp->assign ("Verifycode", $data [' Verifycode ']); CI framework in template files using the original eco-PHP syntax output data//$this->load->view (' login ', $data);//landing page, note: Parameter 2 needs to appear as an array//
  Displays the template file that is set by the Smarty template engine $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 ']): '; Verify code//Verify Code verification if ($verifycode = = $this->session->uSerdata (' Verifycode ')) {//obtain user information based on username and password, note: Parameter 2 is an encrypted password $user _info= $this->user_model->check_user_login ($
    USERNAME,MD5 ($password));
     if ($user _info[' user_id '] > 0) {//Insert User ID, username, password into the cookie//the first way to set the cookie: The value of the cookie set by the native 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");//Applicable 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 setting C Ookie the way: ThroughCI Framework cookie_helper.php function library File//This approach is not very effective, we recommend that you take a second way to//set_cookie ("username", $user _info[' username '],3600);
     echo Get_cookie ("username");
     Session Login to use: 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 Landing Page header ("Location:". Site_url ("Common/login"));

 }
  }
 }
}

Step two: user_model.php

Cookie Login: Detect whether the user login, if the cookie value is invalid, return false, if the cookie value is not invalidated, then according to the user name and password in the cookie from the database to obtain user information, if you can obtain user information, then return the query to the user information, If no user information is queried, return 0 public function Is_login () {//Get 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; To obtain user information from the database based on user name and encrypted password, and if so, return the obtained user information, otherwise return false, note: Password is encrypted password public function Check_user_login ($username, $
  Password) {//Here everyone should note: $password for MD5 encrypted password//$this->db->query ("select * from"); The use of shortcut query classes: can provide us with fast access to data//the array is a query condition//NOTE: Associative array $arr =array (' username ' => $username,//user name ' password ' => $pas
  sword,//encryption password ' status ' =>1//account open;
  The data table prefix is already 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:

The Public Function __construct () {//invokes the constructor of the parent class parent::__construct (); $this->load->library (' TP '); Smarty template Parsing class $this->load->helper (' url '); The URL function library file $this->load->model ("User_model"),//user_model the 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 already logged in, reset the validity period of the cookie $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");//referencing Operation_model model $this->load->model ("Object_model");//Referencing Object

 _model model $this->load->model ("Permission_model");//reference Permission_model model}

More interested in CodeIgniter related content readers can view the site topics: "CodeIgniter Introductory Course", "CI (CodeIgniter) Framework Advanced Course", "PHP Excellent Development Framework Summary", "thinkphp Introductory Course", " Thinkphp Common Methods Summary, "Zend Framework Introduction Course", "PHP object-oriented Programming Introduction Course", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on CodeIgniter 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.