[英]How to login in codeigniter?
I have syntax, I think the syntax already correct but when I run in browser the login not enter to form the success page.我有语法,我认为语法已经正确,但是当我在浏览器中运行时,登录不输入以形成成功页面。 the login just repeats in
form_login
.登录只是在
form_login
中重复。 if I input username and password wrong are the same nothing notification error.如果我输入用户名和密码错误是一样的没有通知错误。 what's wrong the syntax I made?
我做的语法有什么问题?
this is the controller这是 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');
}
this is html login这是 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>
this is the models这是模型
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();
}
use Ion Auth 3 library Makes it easier for you使用 Ion Auth 3 库让您更轻松
https://github.com/benedmunds/CodeIgniter-Ion-Auth https://github.com/benedmunds/CodeIgniter-Ion-Auth
But:但:
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');
}
}
models:楷模:
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;
}
I'm sorry I didn't have enough time to test, but I think it's working right now很抱歉我没有足够的时间进行测试,但我认为它现在可以正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.