[eluser]chaminda[/eluser]
i am trying to create a login system with CI. what i want to do is display an error saying "You entered an incorrect username or password", when user enter a non existed username and password.
So here is the problem. when i enter non existed username and password no error message were displayed. but when i try entering again an none existed username and password the error message is displaying.
i think this is something to do with the flashdata's
"only be available for the next server request" behavior.
i can't figure out how to fix this. when i try to use
userdata instead of the
flashdata, i am always getting the "You entered an incorrect username or password" error whether my form submitted or not. i think this happen because the userdata session doesn't get automatically cleared like flashdata.
Here is my login function from the main controller
---------------------------------------------------
Code:
function login()
{
$data['records'] = $this->main_model->get_records();
$data['total'] = $this->main_model->count_records();
$this->form_validation->set_rules('username','Username','required|trim|maxlength[50]|xss_clean');
$this->form_validation->set_rules('password','Password','required|trim|maxlength[200]|xss_clean');
if($this->form_validation->run() == FALSE)
{
$this->load->view('index',$data);
}
else
//Process the form and log in the user
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$userid = $this->main_model->get_user($username, $password);
if(!$userid)
{
//User not avilable
$this->session->set_flashdata('login_failed',TRUE);
$this->load->view('index',$data);
}
else
{
//User avilable. log him in
$login_data = array('logged_in' => TRUE, 'username' => $username);
$this->session->set_userdata($login_data);
$this->load->view('index',$data);
}
}
}
here is my index view
------------------------
Code:
<div id="logpanel">
<?php if ($this->session->userdata('username'))
{
echo 'welcome ' . $this->session->userdata('username') . '<br><a href="'. base_url().'main/logout">logout</a>';
}
else{
?>
<?php echo form_open(base_url(). 'main/login')?>
<div id="username">
<label>Username:</label>
<?php echo form_input(array('id' => 'username', 'name' => 'username')); ?>
</div>
<div id="password">
<label>Password:</label>
<?php echo form_password(array('id' => 'password', 'name' => 'password')); ?>
</div>
<?php echo form_submit(array('name' => 'submit'), 'Login') . ' or signup from here'?>
<?php
if($this->session->flashdata('login_failed'))
{
echo '<br />' . 'You entered an incorrect username or password';
}
?>
<?php echo validation_errors(); ?>
<?php echo form_close(); }?>
</div>