DMaguire
DMaguire

Reputation: 1

Flash data not appearing in Codeigniter?

I am currently devising a login/registration system with codeigniter. Everything seems to be in order apart from the Flash Data when I enter incorrect password or username.

This is what I'm trying to achieve, flash data to appear if incorrect credentials are entered by a user.

Here is the code for my Controller:

<?php
        //Extending the base controller
        class Auth extends CI_Controller{


       //calling the login view
       public function login(){
       //Only username and password credentials are needed here
        $this->form_validation->set_rules('username','Username','required');//using form_validation library
      $this->form_validation->set_rules('password','Password','required|min_length[5]');

      if($this->form_validation->run() == TRUE){

        $username = $_POST['username'];
        $password = md5($_POST['password']);//password hashing

        //Search for user in db
        $this->db->select('*');//query
        $this->db->from('users');
        $this->db->where(array('username' =>$username, 'password' => $password));
        $query = $this->db->get();//store in a var called query
        $user = $query->row();

        if($user->email){
          //temp message
          $this->session->set_flashdata("success","You are now logged in");

          //session variables
           $_SESSION['user_logged'] = TRUE;
           $_SESSION['username'] = $user->username;
           $_SESSION['email'] = $user->email;
           $_SESSION['password'] = $user->email;

           //redirect to their own profile page
           redirect("user/profile", "refresh");
        }else{
          $this->session->set_flashdata("error","No account exists, why not register with us?");
          redirect("auth/login", "refresh");

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

Upvotes: 0

Views: 367

Answers (2)

Yadhu Babu
Yadhu Babu

Reputation: 1521

load library session in your controller

$this->load->library('session');

In your controller

$this->session->set_flashdata('error', 'No account exists, why not register with us?');
redirect("auth/login");

In your view

<?php if($this->session->flashdata('error')){ ?>
    <h4 style="color:red;"><?php echo $this->session->flashdata('error'); ?></h4>

<?php } ?>

Upvotes: 1

user4419336
user4419336

Reputation:

Remove this , "refresh" because it clears it or you can use to keep it for certain time.

https://www.codeigniter.com/user_guide/libraries/sessions.html#tempdata

// Both 'item' and 'item2' will expire after 300 seconds
$this->session->mark_as_temp(array('item', 'item2'), 300);

// 'item' will be erased after 300 seconds, while 'item2'
// will do so after only 240 seconds
$this->session->mark_as_temp(array(
        'item'  => 300,
        'item2' => 240
));

Also just a tip it looks like your not using any hashing for your passwords. I would use http://php.net/manual/en/function.password-hash.php and to verify it http://php.net/manual/en/function.password-verify.php

And setting sessions https://www.codeigniter.com/user_guide/libraries/sessions.html#adding-session-data make sure you have set your session save path in config.php etc

Upvotes: 0

Related Questions