Welcome Guest, Not a member yet? Register   Sign In
Unexpected flashdata behavior. Please help !
#1

[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">
  
&lt;?php if ($this->session->userdata('username'))
{
    echo 'welcome ' . $this->session->userdata('username') . '<br><a href="'. base_url().'main/logout">logout</a>';
}
else{
?&gt;
        &lt;?php echo form_open(base_url(). 'main/login')?&gt;
        <div id="username">
        <label>Username:</label>
        &lt;?php echo form_input(array('id' => 'username', 'name' => 'username')); ?&gt;
        </div>
        
        <div id="password">
        <label>Password:</label>
        &lt;?php echo form_password(array('id' => 'password', 'name' => 'password')); ?&gt;
        </div>
        &lt;?php echo form_submit(array('name' => 'submit'), 'Login') . '  or signup from here'?&gt;
        &lt;?php
        if($this->session->flashdata('login_failed'))
            {
                echo '<br />' . 'You entered an incorrect username or password';
            }
         ?&gt;
        &lt;?php echo validation_errors(); ?&gt;
        &lt;?php echo form_close(); }?&gt;
        </div>
#2

[eluser]chaminda[/eluser]
sorted it out. i just added simple redirect before the login function's view. So the flash data available for my view because it is the next request.

Code:
$this->session->set_flashdata('login_failed',TRUE);
            redirect('main/login');
            $this->load->view('index',$data);




Theme © iAndrew 2016 - Forum software by © MyBB