1

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.

Pradeep
  • 9,667
  • 13
  • 27
  • 34

2 Answers2

4

please add echo statement in the view like

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

OR

<?=$this->session->flashdata('msg')?>
atta afridi
  • 111
  • 5
1

It should be like this :

Get your flashdata by using its key, Should be like this

<?php if(!empty($this->session->flashdata('msg'))) {?> 
    <div class="alert alert-danger">
       <?php echo $this->session->flashdata('msg'); ?>
    </div>
<?php } ?>

Or simply do like this:

<div class="alert alert-success"><?php echo $this->session->flashdata('msg'); ?></div>

For more : https://www.codeigniter.com/user_guide/libraries/sessions.html

Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • 1
    Cannot use isset() on the result of a function call (you can use "null !== func()" instead) in D:\xamp\htdocs\login_pagination\application\views\login_v.php on line 24 I am getting this error now –  Jul 18 '18 at 15:25
  • change `isset` to `empty` , just updated my answer pls check it – Pradeep Jul 18 '18 at 16:15