登录后如何回显用户

时间:2019-12-10 09:09:29

标签: codeigniter authentication user-controls echo textfield

强文本,我想回显创建数据的用户,但似乎有可能吗?有人知道此问题吗?

  

这是我的控制器   

类Auth扩展了CI_Controller {

public function index(){
    $data['users'] = $this->User_model->get();
    $this->load->view('login_page');
}

public function login(){
    $email = $_POST['email'];
    $password = $_POST['password'];

    $data = $this->User_model->login ($email, $password);

    if($data){
        $this->session->set_userdata('user', $data);
        redirect('Users');
    }
    else{
        header('location:'.base_url().$this->index());
        Echo 'error';
    } 
}

}

  

这是我的模特   

User_model类扩展了CI_Model {

private $_table = "users";


public function get( $id = false )
{

    if ($id) {
        $this->db->where('id =', $id);
        $query = $this->db->get($this->_table);
        return $query->row_array();
    }

    $query = $this->db->get($this->_table);    
    return $query->result_array();
}

public function insert($data)
{ 
    $data['password'] = password_hash($data['password'],PASSWORD_DEFAULT);
    $this->db->insert($this->_table, $data);

}

public function update($data)
{
    $id = $data['id'];
    unset($data['id']);
    $data['password'] = password_hash($data['password'],PASSWORD_DEFAULT);
    $this->db->where('id =', $id);
    $this->db->update($this->_table, $data);
}

public function delete($id)
{
    $this->db->where('id', $id);
    $this->db->delete($this->_table);
}

public function login($email, $password){
    $query = $this->db->get_where('users', array('email'=>$email, $data['password'] = password_hash($data['password'],PASSWORD_DEFAULT))) ;

    return $query->row_array();
}

}

  

这是我的视图,我要在下面的文本框创建的用户中回显该用户。有人有主意吗?

<< / p>

!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Roles</title>
    </head>
    <body>
      <h1>Roles</h1>
       <div class="container">


       <form method="post" action="<?php echo site_url('roles/add')?>" >


          <label>Code</label>
          <input type="text"  name="code" placeholder="Code">


          <label>Description</label>
          <input type="text" name="description" placeholder="Description">

        <label>Permissions</label>
        <input type="text" name="permissions" placeholder="Permissions">

              <!--  created By -->
          <label>Created by</label>
          <input type="text"  name="created_by" placeholder="Name">

        <button type="submit"  value="save" >Submit</button>
    </form>
</div>

  </body>
</html>

2 个答案:

答案 0 :(得分:0)

在您的控制器中使用如下方法:

$data = $this->User_model->login($email, $password);

现在处于模态:

public function login($email,$password){
 $password = password_hash($password,PASSWORD_DEFAULT);
        $query = $this->db->get_where('users', array('email'=>$email, 'password'=>$password));

        return $query->row_array();
    }

希望它会有所帮助:)

答案 1 :(得分:0)

尝试以下

您的模型

public function login($email,$password){

    $query = $this->db
        ->from('users')
        ->where('email', $email)
        ->get();

    if ($query->num_rows() !== 1)   throw new Exception('Invalid User');

    $arrUser = $query->row_array();

    if (!password_verify($password, $arrUser['password']))  throw new Exception('Invalid Password');

    return $arrUser;
}

和您的控制器

public function login(){

    try

    {
        //load session library
        $this->load->library('session');

        $email = $_POST['email'];
        $password = $_POST['password'];

        $this->session->set_userdata('user', $this->User_model->login password_verify($email, $password));
        redirect('Users');

    }
    catch(\Throwable $e)
    {
        $this->session->set_flashdata('error', $e->getMessage());
        header('location:'.base_url('user'));
    }
}