Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter flashdata delay

Tags:

codeigniter

I'm tring to validate a login form and sending error messages using flashdata. Flashdata seems to have a delay, it's only affected a second submit (no error message is shown after first submit, ok after second), whereas userdata works correctly.

View>

<?php if ($this->session->flashdata('error_messages')): ?>
  <?php echo $this->session->flashdata('error_messages'); ?>
<?php endif; ?>
<?php echo form_open('user/login', array('name' => 'login_form', 'id' => 'login_form')); ?>
<fieldset>
  <p>
    <?php echo form_label('Username: ', 'username'); ?>
    <?php echo form_input('username', set_value('username', ''), array('id' => 'username', 'placeholder' => 'username')); ?>
  </p>
  <p>
    <?php echo form_label('Password: ', 'password'); ?>
    <?php echo form_password('password', set_value('password', ''), array('id' => 'password', 'placeholder' => '********')); ?>
 </p>
 <p>
   <?php echo form_submit('login', 'Login', array('id' => 'login')); ?>
 </p>

Controller action>

public function login() {
    //user is not logged in, carry on
    if (!user_is_logged_in()) {
        //check if form was already submitted, if yes validate
        if ($this->input->post('login')) {
            $this->load->library('chronos_user');

            //gather data from form
            $username = $this->input->post('username');
            $password = $this->input->post('password');

            //validate form, show error messages if not valid
            if (
                !($this->chronos_user->valid_username($username)) ||
                !($this->chronos_user->unique_username($username))
            )
            {
                $this->chronos_template->set_data('header', array('controller' => 'users', 'action' => 'login', 'title' => 'Login'));
                $this->chronos_template->set_view('content', 'user/login_view');
                $this->chronos_template->view();
            }
            //redirect if ok
            else {
                header("Location http://c4studio.ro");
            }
        }
        //if no form was submitted, just display it
        else {
            $this->chronos_template->set_data('header', array('controller' => 'users', 'action' => 'login', 'title' => 'Login'));
            $this->chronos_template->set_view('content', 'user/login_view');
            $this->chronos_template->view();            
        }
    }
    //user is already logged in, redirect to profile page
    else {
        $profile_url = "Location: ";
        $profile_url .= site_url();
        $profile_url .= "user/profile/";

        header($profile_url);
    }
}

Valid_username method>

    public function valid_username($username) {
        if (empty($username)) {

            $this->CI->session->set_flashdata('error_messages', 'Value.');
            return FALSE;
        }
        if (strlen($username) < 3) {
            $this->CI->session->set_flashdata('error_messages', 'Value11.');
            return FALSE;
        }
        return TRUE;
    }

Any ideas? Also if somebody knows a better way to do all this please let me know! Thanks

like image 380
Illes Peter Avatar asked Jun 12 '26 15:06

Illes Peter


1 Answers

According to the documentation flashdata exists at the next server request.

But in your case you actually set the flashdata in the same server request as you show the login view which was unsuccesful. Thats the reason it doesn't work.

A better way wuld to just allow your model method to return true or false and use that value instead. The flashdata should only be considered if you redirect the user to an error page or something like that.

like image 75
Repox Avatar answered Jun 14 '26 11:06

Repox