Search code examples
phpcodeignitercodeigniter-3codeigniter-flashdata

codeigniter, set_flashdata not working


I read on stack overflow about flash data only valid till next server request, therefore I made new flashdata for couple of message display.. here below is the my code

This is my controller Controller

public function login(){
        $this->form_validation->set_rules('username','Username','required');
        $this->form_validation->set_rules('password','Password','required|min_length[5]');


        if($this->form_validation->run() == TRUE){
            $username= $this->input->post('username');
            $password= $this->input->post('password');

            $this->load->model('Auth_model');
            $user = $this->Auth_model->get_login();

           if ($user == 0) {
                    //echo "<script>alert('wrong username');</script>";

                    $this->session->set_flashdata("msg","Username does not exists");
                    redirect("auth/login");
           }
           else{
            print_r($user['username']); 
            if($username == $user['username'] && $password == $user['password']){
                $this->session->set_flashdata("success","You are logged in");
                $_SESSION['user_logged'] = TRUE;
                $_SESSION['username'] = $user['username'];
                redirect("user/dashboard","refresh");
            }
            else {

                //echo "<script>alert('wrong password');</script>";
                $this->session->set_flashdata("msg","Password does not match.");
                redirect("auth/login");
            }

        }
        }
        $this->load->view('login_v');
    }

Model

public function get_login(){

        $username = $this->security->xss_clean($this->input->post('username'));
        $password = $this->security->xss_clean($this->input->post('password'));

            $this->db->select('*');
            $this->db->from('users');
            $this->db->where(array('username' => $username));
            $query = $this->db->get();

            $user = $query->row();

            if ($this->db->affected_rows() != 1) {
                return false;
            } 
            else {
            $data = array(
                'user_id' => $user->user_id,
                'username' => $user->username,
                'password' => $user->password
            );
            //print_r($data);
            //$this->session->set_userdata($data);
            return $data;
        }
    }

view

  <?php if(isset($_SESSION['success'])) {?>
    <div class="alert alert-success"><?php echo $_SESSION['success']; ?></div>
  <?php } ?>
  <?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
  <?php $this->session->flashdata('msg');?>
  <form action="" method="POST">
    <div class="form-group">
      <label for="username">Username</label>
      <input type="text" class="form-control" name="username" id="username">
    </div>
    <div class="form-group">
      <label for="password">Password:</label>
      <input type="password" class="form-control" name="password" id="password">
    </div>
    <div>
      <button class="btn btn-primary" name="login">Login</button>
    </div>
  </form>

I want to display

$this->session->set_flashdata("msg","Username does not exists");

but my if else is just doing redirect, the commented script tag works fine though.

How to make "msg" work?

Thanks in advance.


Solution

  • please add echo statement in the view like

     <?php echo $this->session->flashdata('msg');?>
    

    OR

    <?=$this->session->flashdata('msg')?>