CodeIgniter, set_flashdata not working after redirect

codeigniterflashsession

set_flashdata is not working directly after redirect with only one redirect.

I am using one controller in this process – Profilers' Controller. It handles the member confirmation process and also displays the login page on the redirect. The process is as follows:

  1. this session set_flashdata ('topic', 'newmember')

  2. redirect ('login')

  3. route ['login'] = 'profilers/signIn'

  4. topic = $this session flashdata ('topic')

I have turned off all database session configuration for cleaner debugging and even though session library is turned on in configs, I have started calling it anyways which doesn't seem to work either.

Here is my code. As you can see, I am sending path info to a log file path.log:

in controller Profilers, function confirmMember:

public function confirmMember()
  {
    //use_ssl();
    $this->form_validation->set_rules('handle', 'Unique Member Name', 'trim|xss_clean|required|min_length[5]|max_length[30]');
    $this->form_validation->set_rules('confirmation', 'Confirmation Code', 'trim|xss_clean|required|min_length[20]|max_length[20]|alpha_numeric');

    if ($this->form_validation->run() === FALSE) {echo "here";exit;
      $data['handle']=$this->input->post('handle');
      $data['confirmation']=$this->input->post('confirmation');
      $this->load->view('signing/defaults/header',$data);
      $this->load->view('defaults/heading',$data);
      $this->load->view('defaults/banner');
      $this->load->view('defaults/banner_right');
      $this->load->view('member/temp/index',$data);
      $this->load->view('defaults/footer',$data);
    } else {

      $post = $this->input->post(NULL,TRUE);
      $data['member'] = $this->Signing_model->model_confirmMember($post);

  if ($data['member']['confirmed']!==FALSE) {
    /* PATH CHECK */ 
    error_log("member confirmation not false\n",3, LOG_DIR.'path.log');
    unset($post);
    $this->session->sess_destroy();
    $this->session->set_flashdata('topic', 'newmember');
    // $this->session->keep_flashdata('topic');
    redirect('login','refresh');
  } else {
    /* PATH CHECK */ 
    error_log("member confirmation IS FALSE\n",3, LOG_DIR.'path.log');
    $this->load->view('member/temp/index',$data);
  }

My log file shows that the path is using the correct path and showing "member confirmation not false".

I have tried with keep_flash data on (which I assumed wouldn't work since there are no other redirects) and off.

I have also tried redirect without 'refresh'.

In config/routes.php:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$route['join'] = 'profilers/joinUp';
$route['login'] = 'profilers/signIn';
...

Login page uses Profilers Controller, signIn function as show above:

public function signIn()
{
  $topic = $this->session->flashdata('topic');
  if (isset($topic)) {
      $message = "topic is set. topic = ".$topic."\n";
      if ($topic!==FALSE) {
              error_log("flash var topic is not false\n", 3, LOG_DIR.'path.log');
      } else {
              error_log("flash var topic is FALSE\n", 3, LOG_DIR.'path.log');
      }
  } else {
      $message = "topic is NOT set\n";
  }
  error_log($message,3,LOG_DIR.'path.log');

  exit;
  ...
  ...
}

log file is showing that topic is set but is false.

"flash var topic is FALSE"

"topic is set. topic = "

Of course topic var not set since it is FALSE.

As you can see, I have moved the get flash data function to the beginning of my controller function to bypass anything that may be corrupting data.

Best Answer

You may need to start the session again after you have destroyed it.

Try adding this after your call to sess_destory():

$this->session->sess_create()

Alternatively you could avoid destroying the session, and unset() the values you wish to get rid of.

Related Topic