[eluser]Unknown[/eluser]
I have a login form that is set to return a user to the login page if the email/password combo is not found in the database, but the error message I am attempting to set up through flashdata only shows up after a second consecutive unsuccessful login attempt.
Here is my controller:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
function validate_credentials()
{
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query) // if the user's credentials validated...
{
$data = array(
'user_email' => $this->input->post('user_email'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site/members_area');
}
else // incorrect username or password
{
$this->session->set_flashdata('message', 'The email or password is incorrect.');
$this->index();
}
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
}
/* End of file login.php */
/* Location: ./application/controllers/login.php */
And here is my view:
Code:
<div id="content">
<h1>Login</h1>
<div id="login_form">
<?php echo form_open('login/validate_credentials'); ?>
<h5>Email Address</h5>
<?php echo form_error('user_email'); ?>
<input type="text" name="user_email" value="<?php echo set_value('user_email'); ?>" size="50" />
<h5>Password</h5>
<?php echo form_error('user_pass'); ?>
<input type="password" name="user_pass" size="50" />
<?php if($this->session->flashdata('message')) : ?>
<p class="error"><?=$this->session->flashdata('message')?></p>
<?php endif; ?>
<div><input type="submit" value="Submit" /></div>
<?php echo form_close(); ?>
</div><!-- end login_form-->
</div><!--end content-->
I also load this model from the controller:
Code:
<?php
class Membership_model extends CI_Model {
function validate()
{
$this->db->where('user_email', $this->input->post('user_email'));
$this->db->where('user_pass', md5($this->input->post('user_pass')));
$query = $this->db->get('users');
if($query->num_rows == 1)
{
return true;
}
}
}
I would like the error to appear after the first and any subsequent unsuccessful attempts> Any thoughts?
Thanks!