我是CodeIgniter的新手,正在尝试创建一个登录页面。在我的情况下,我想如果错误的密码或电子邮件得到闪光数据,如果真的会转到另一个页面。我试图输入错误的电子邮件和密码,但它只是刷新页面而不显示flash数据,我试图输入正确的电子邮件和口令,它只是刷新网页而不重定向到另一个页面。
这是我的模型代码(M_login.php(:
public function get_user_by_email($user_email)
{
$data = $this->db->select('*')
->from('user')
->where('user_email', $user_email)
->get()
->row();
return $data;
}
我的控制器(Login.php(:
public function __construct()
{
parent::__construct();
$this->load->model('M_login');
}
public function login()
{
$data['main_content'] = $this->load->view('template/login', '', true);
$this->load->view('template/layout', $data);
}
public function customer_login()
{
$user_email = $this->input->post('user_email', true);
$user_pass = md5($this->input->post('user_pswd', true));
$user_detail = $this->M_login->get_user_by_email($user_email);
if ($user_pass == $user_detail->user_pswd) {
$sdata['user_id'] = $user_detail->user_id;
$sdata['user_name'] = $user_detail->user_name;
$sdata['user_email'] = $user_detail->user_email;
$sdata['user_id'] = $this->session->set_userdata($sdata);
redirect("billing");
} else {
$this->session->set_flashdata('flash_msg', 'Password atau Email anda salah........!');
redirect("Login/login");
}
}
我的视图(login.php(:
<form action=" <?= base_url() ?>customer_login" method="post">
<section class=" my-3">
<input class="input is-rounded is-medium" type="email" name="user_email" placeholder="Masukan e-mail anda">
</section>
<section class="my-3">
<input class="input is-rounded is-medium" type="password" name="user_pswd" placeholder="Masukkan password anda">
</section>
<section class="mt-4 ml-2">
<button class="button is-danger" type="submit">Login</button>
</section>
</form>
这是我的数据库:数据库用户
视图(login.php(:-
<form action="<?php echo base_url();?>Login/customer_login" method="POST">
<?php
if( $this->session->flashdata('err_message') )
{
echo $this->session->flashdata('err_message');
}
?>
<section class=" my-3">
<input class="input is-rounded is-medium" type="email" name="user_email" placeholder="Masukan e-mail anda">
</section>
<section class="my-3">
<input class="input is-rounded is-medium" type="password" name="user_pswd" placeholder="Masukkan password anda">
</section>
<section class="mt-4 ml-2">
<button class="button is-danger">Login</button>
</section>
</form>
控制器(Login.php(:-
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('M_login');
}
public function login()
{
$data['main_content'] = $this->load->view('template/login', '', true);
$this->load->view('template/layout', $data);
}
public function customer_login()
{
$user_email = $this->input->post('user_email', true);
$user_pass = md5($this->input->post('user_pswd', true));
$user_result = $this->M_login->get_user($user_email,$user_pass);
if ($user_result >0) //active user record is present
{
$this->session->set_userdata('user_session',$user_result);
$this->session->set_flashdata('login_message', '<div class="alert alert-success text-center">You are Successfully Login to your account!</div>');
$url = base_url('Login/billing');
redirect($url);
} else {
$this->session->set_flashdata('err_message', '<div class="alert alert-danger text-center">Invalid email and password!</div>');
redirect("Login/login");
}
}
public function billing()
{
$this->load->view('user_billing_dashboard');
}
}
型号M_login:-
class M_login extends CI_Model {
public function get_user($user_email,$user_pass)
{
$data = $this->db->get_where('user',array('user_email'=>$user_email,'user_pswd'=>$user_pass))->row_array();
return $data;
}
}
创建视图:-
user_billing_dashboard.php
<div class="col-md-12">
<?php
if($this->session->flashdata('login_message')){
?>
<div class="alert alert-success" style="display:none;">
<?php echo $this->session->flashdata('login_message'); ?>
<?php
} else if($this->session->flashdata('error')){
?>
<div class = "alert alert-danger" style="display:none;">
<?php echo $this->session->flashdata('error'); ?>
</div>
<?php } ?>
</div>