Home  >  Article  >  Backend Development  >  Detailed explanation of how to implement cookie login in CI framework, detailed explanation of cookie in CI framework_PHP tutorial

Detailed explanation of how to implement cookie login in CI framework, detailed explanation of cookie in CI framework_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:52:13967browse

Detailed explanation of how CI framework implements cookie login. Detailed explanation of ci framework cookie

The example in this article describes how CI framework implements cookie login. Share it with everyone for your reference, the details are as follows:

Step one: login.php

//登陆方法
 public function login(){
  //如果用户名和密码为空,则返回登陆页面
  if(empty($_POST['username']) || empty($_POST['password'])){
   $data['verifycode'] = rand(1000,9999);//生成一个四位数字的验证码
   //将验证码放入session中,注意:参数是数组的格式
   $this->session->set_userdata($data);
   //注意:CI框架默认模板引擎解析的模板文件中变量不需要$符号
   //$this->parser->parse("admin/login",$data);
   //smarty模板变量赋值
   $this->tp->assign("verifycode",$data['verifycode']);
   //ci框架在模板文件中使用原生态的PHP语法输出数据
   //$this->load->view('login',$data);//登陆页面,注意:参数2需要以数组的形式出现
   //显示smarty模板引擎设定的模板文件
   $this->tp->display("admin/login.php");
  }else{
   $username = isset($_POST['username'])&&!empty($_POST['username'])?trim($_POST['username']):'';//用户名
   $password = isset($_POST['password'])&&!empty($_POST['password'])?trim($_POST['password']):'';//密码
   $verifycode = isset($_POST['verifycode'])&&!empty($_POST['verifycode'])?trim($_POST['verifycode']):'';//验证码
   //做验证码的校验
   if($verifycode == $this->session->userdata('verifycode')){
    //根据用户名及密码获取用户信息,注意:参数2是加密的密码
    $user_info=$this->user_model->check_user_login($username,md5($password));
    if($user_info['user_id'] > 0){
     //将用户id、username、password放入cookie中
     //第一种设置cookie的方式:采用php原生态的方法设置的cookie的值
     //setcookie("user_id",$user_info['user_id'],86500);
     //setcookie("username",$user_info['username'],86500);
     //setcookie("password",$user_info['password'],86500);
     //echo $_COOKIE['username'];
     //第二种设置cookie的方式:通过CI框架的input类库
     $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");//适用于控制器
     //echo $this->input->cookie("username");//适用于控制器
     //echo $_COOKIE['username'];//在模型类中可以通过这种方式获取cookie值
     //echo $_COOKIE['password'];//在模型类中可以通过这种方式获取cookie值
     //第三种设置cookie的方式:通过CI框架的cookie_helper.php函数库文件
     //这种方式不是很灵验,建议大家采取第二种方式即可
     //set_cookie("username",$user_info['username'],3600);
     //echo get_cookie("username");
     //session登陆时使用:将用户名和用户id存入session中
     //$data['username']=$user_info['username'];
     //$data['user_id']=$user_info['user_id'];
     //$this->session->set_userdata($data);
     //跳转到指定页面
     //注意:site_url()与base_url()的区别,前者带index.php,后者不带index.php
     header("location:".site_url("index/index"));
    }
   }else{
    //跳转到登陆页面
    header("location:".site_url("common/login"));
   }
  }
 }
}

Step 2: User_model.php

//cookie登陆:检测用户是否登陆,如果cookie值失效,则返回false,如果cookie值未失效,则根据cookie中的用户名和密码从数据库中获取用户信息,如果能获取到用户信息,则返回查询到的用户信息,如果没有查询到用户信息,则返回0
 public function is_login(){
  //获取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;
 }
 //根据用户名及加密密码从数据库中获取用户信息,如果能获取到,则返回获取到的用户信息,否则返回false,注意:密码为加密密码
 public function check_user_login($username,$password){
  //这里大家要注意:$password为md5加密后的密码
  //$this->db->query("select * from ");
  //快捷查询类的使用:能为我们提供快速获取数据的方法
  //此数组为查询条件
  //注意:关联数组
  $arr=array(
   'username'=>$username,//用户名
   'password'=>$password,//加密密码
   'status'=>1   //账户为开启状态
  );
  //在database.php文件中已经设置了数据表的前缀,所以此时数据表无需带前缀
  $query = $this->db->get_where("users",$arr);
  //返回二维数组
  //$data=$query->result_array();
  //返回一维数组
  $user_info=$query->row_array();
  if(!empty($user_info)){
   return $user_info;
  }else{
   return false;
  }
}

Step 3: Other controllers:

public function __construct(){
  //调用父类的构造函数
  parent::__construct();
  $this->load->library('tp'); //smarty模板解析类
  $this->load->helper('url'); //url函数库文件
  $this->load->model("user_model");//User_model模型类实例化对象
  $this->cur_user=$this->user_model->is_login();
  if($this->cur_user === false){
   header("location:".site_url("common/login"));
  }else{
   //如果已经登陆,则重新设置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');//分页类库
  $this->load->model("role_model");//member_model模型类
  $this->load->model("operation_model");//引用operation_model模型
  $this->load->model("object_model");//引用object_model模型
  $this->load->model("permission_model");//引用permission_model模型
}

Readers who are interested in more CodeIgniter related content can check out the special topics of this site: "codeigniter introductory tutorial", "CI (CodeIgniter) framework advanced tutorial", "php excellent development framework summary", "ThinkPHP introductory tutorial", "Summary of Common Methods in ThinkPHP", "Introduction Tutorial on Zend FrameWork Framework", "Introduction Tutorial on PHP Object-Oriented Programming", "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"

I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1127858.htmlTechArticleDetailed explanation of the method of cookie login in CI framework, detailed explanation of cookie login in ci framework. This article describes the method of cookie login in CI framework. . Share it with everyone for your reference, the details are as follows: The first step...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn