强文本我想回显创建数据的用户,但似乎有可能?有人知道这个问题吗?
这是我的控制器
类身份验证扩展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>
在控制器中使用如下方法:
$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();
}
希望对:)有所帮助
请尝试以下操作
您的模型
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'));
}
}