Welcome Guest, Not a member yet? Register   Sign In
Login Validation Error
#1

[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">
        
            &lt;?php echo form_open('login/validate_credentials'); ?&gt;
            
                <h5>Email Address</h5>
                &lt;?php echo form_error('user_email'); ?&gt;
                &lt;input type="text" name="user_email" value="&lt;?php echo set_value('user_email'); ?&gt;" size="50" /&gt;
                
                <h5>Password</h5>
                &lt;?php echo form_error('user_pass'); ?&gt;
                &lt;input type="password" name="user_pass" size="50" /&gt;
                
                &lt;?php if($this->session->flashdata('message')) : ?&gt;
                    <p class="error">&lt;?=$this->session->flashdata('message')?&gt;</p>
                &lt;?php endif; ?&gt;
            
                <div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>
            
            &lt;?php echo form_close(); ?&gt;

        </div>&lt;!-- end login_form--&gt;
        
    </div>&lt;!--end content--&gt;

I also load this model from the controller:
Code:
&lt;?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!
#2

[eluser]Kindari[/eluser]
Calling $this->index() just executes the index code, does NOT make another request. Flashdata shows up on the next *request*.

So instead of calling $this->index() do redirect('Login/index');
#3

[eluser]Unknown[/eluser]
Thank you! Worked perfectly. Much appreciated.




Theme © iAndrew 2016 - Forum software by © MyBB