登录后如何回显用户

问题描述 投票:-1回答:2

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

这是我的控制器

类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();
}

}

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

<

!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>
codeigniter authentication user-controls echo textfield
2个回答
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();
    }

希望它会有所帮助:)


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'));
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.