好的,所以在我的数据库中我有一个名为'users'的表,包含电子邮件,pw和其他一些字段。注册工作。但是我无法登录。
控制器视图:
public function index()
{
$this->form_validation->set_rules('email2', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('pw2', 'Hasło', 'trim|required|md5');
$this->load->view('header_view');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('account_view');
}
else
{
$email = $this->input->post('email2');
$password = $this->input->post('pw2');
$this->load->model('login_model');
if($user = $this->login_model->login_user($email, $password))
{
$this->session->set_userdata('user_id', $user['id']);
redirect('home');
}
else
{
redirect('account');
}
模型视图: class login_model扩展了CI_Model { public $ table ='users';
public function login_user($email, $pw)
{
return $this->db->where(array('email' => $email, 'pw' => $pw))->get($this->table)->row_array();
}
查看:
<p>
<?php echo form_error('email2'); ?>
<input type="text" id="email2" name="email2" value="<?php echo set_value('email2'); ?>" />
<label for="email2">Email</label>
</p>
<p>
<?php echo form_error('pw2'); ?>
<input type="password" id="pw2" name="pw2" />
<label for="pw2">Hasło</label>
</p>
<p>
<input type="submit" value="Zaloguj" />
</p>
<?php echo form_close(); ?>
</form>
答案 0 :(得分:0)
在您的控制器中:
public function index()
{
$this->form_validation->set_rules('email2', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('pw2', 'Hasło', 'trim|required|md5');
$this->load->view('header_view');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('account_view');
}
else
{
redirect('home','refresh');
}
function check_database($password)
{
//Field validation succeeded. Validate against database
$username = $this->input->post('email2');
//query the database
$result = $this->user->login($email2, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->id,
'email2' => $row->email2
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
在你的模特中:
Class User extends CI_Model
{
function login($username, $password)
{
$this -> db -> select('id, email, pw');
$this -> db -> from('users');
$this -> db -> where('email', $email2);
$this -> db -> where('pw', MD5($password));
$this -> db -> limit(1);
$query = $this -> db -> get();
if($query -> num_rows() == 1)
{
return $query->result();
}
else
{
return false;
}
}
}