[英]How to login in codeigniter?
我有語法,我認為語法已經正確,但是當我在瀏覽器中運行時,登錄不輸入以形成成功頁面。 登錄只是在form_login
中重復。 如果我輸入用戶名和密碼錯誤是一樣的沒有通知錯誤。 我做的語法有什么問題?
這是 controller
function __construct()
{
parent::__construct();
$this->load->model('account/user_model');
// $this->load->helper('url');
// $this->load->helper('form');
$this->load->library('form_validation');
//tambahin library session supaya mau jalan
$this->load->library('session');
//atau di taruh di autoload
}
//melihat halaman login
public function index()
{
$this->load->view('account/form_login');
}
//memeriksa keberadaan akun username
public function login()
{
$username = $this->input->post('username','true');
$password = $this->input->post('password','true');
$temp_account = $this->user_model->check_user_account($username,$password)->row();
//check account
$num_account = count($temp_account);
$this->form_validation->set_rules('username','username','required');
$this->form_validation->set_rules('password','password','required');
if($this->form_validation->run()==FALSE)
{
$this->load->view('account/form_login');
}
else {
if ($num_account > 0) {
//kalau ada set session
$array_items = array(
'id_user' => $temp_account->id_user,
'username' => $temp_account->username,
'logged_in' => true
);
$this->session->set_userdata($array_items);
redierct(site_url('account/success_page'));
} else {
//kalau ga ada diredirect lagi ke halaman login
$this->session->set_flashdata('notification', 'Peringatan : Username dan Password tidak cocok');
redirect(site_url('account'));
}
}
}
public function view_success_page()
{
$logged_in = $this->session->userdata('logged_in');
if(!$logged_in)
{
redirect(site_url('account'));
}
$this->load->view('account/success_page');
}
這是 html 登錄
<?php echo validation_errors();?>
<p style="color:red"><?php echo $this->session->flashdata('notification')?></p>
<?php echo form_open('account/login')?>
Username : <input typer="text" name="username" value="<?php echo set_value('username')?>"/>
<br>
<br>
Password : <input type="password" name="password" value="<?php echo set_value('password')?>"/>
<br>
<br>
<input type="submit" name="masuk" value="LOGIN"/>
<br>
這是模型
function check_user_account($username,$password)
{
$this->db->select('*');
$this->db->from('user');
$this->db->where('username',$username);
$this->db->where('password',$password);
return $this->db->get();
}
//mengambil data user tertentu
function get_user($id_user)
{
$this->db->select('*');
$this->db->from('user');
$this->db->where('id_user',$id_user);
return $this->db->get();
}
使用 Ion Auth 3 庫讓您更輕松
https://github.com/benedmunds/CodeIgniter-Ion-Auth
但:
public function login()
{
$this->form_validation->set_rules('username','username','required');
$this->form_validation->set_rules('password','password','required');
if($this->form_validation->run()==true)
{
$username = $this->input->post('username','true');
$password = $this->input->post('password','true');
$temp_account = $this->user_model->check_user_account($username,$password);
//check account
if ($temp_account) {
//kalau ada set session
$array_items = array(
'id_user' => $temp_account->id_user,
'username' => $temp_account->username,
'logged_in' => true
);
$this->session->set_userdata($array_items);
redierct(site_url('account/success_page'));
} else {
//kalau ga ada diredirect lagi ke halaman login
$this->session->set_flashdata('notification', 'Peringatan : Username dan Password tidak cocok');
redirect(site_url('account'));
}
}
else {
$this->load->view('account/form_login');
}
}
楷模:
function check_user_account($username,$password)
{
$this->db->select('*');
$this->db->from('user');
$this->db->where('username',$username);
$this->db->where('password',$password);
$q = $this->db->get();
if ($q->num_rows() > 0) {
return $q->row();
} else
return FALSE;
}
很抱歉我沒有足夠的時間進行測試,但我認為它現在可以正常工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.