Reputation: 135
I've try to use $this->session->set_flashdata('success')
and it's not working after redirect to another function. Here is my code:
<?php
class Home extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->helper(array('url','form');
$this->load->library(array('session','template','form_validation');
}
}
/* My another function for form_validation and etc */
public function login(){
$this->set_login_rules();
if($this->form_validation->run()){
/* inserting data to database */
$this->session->set_flashdata('welcome');
redirect('home/welcome');
}
$this->template->display('home');
}
public function welcome(){
if($this->session->flashdata('welcome') !== FALSE){
echo "<script>alert('Flashdata Success! Welcome!</script>";
}
else{
echo "<script>alert('Flashdata Failed! Go Away!');</script>";
}
}
when I run the program, it shows alert Flashdata Failed! Go Away!
but the login data that I want to insert to database is added into the table.
one more thing, sometimes the flashdata
is working. From 10 tries, 8-9 tries if show Flashdata Failed! Go Away!
.
Can anybody tell me why this happend? And how can I fixed it?
Upvotes: 1
Views: 7194
Reputation: 874
From the Codeigniter documentation:
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.
Your problem might be that when you redirect, the process takes more than one request, that clearing your flashdata.
Use regular session or query parameter.
Upvotes: 0
Reputation: 37701
You actually need to give some value to it, so:
$this->session->set_flashdata('welcome');
should be:
$this->session->set_flashdata('welcome', true);
or you can use the full message for example:
$this->session->set_flashdata('welcome', 'Successfully logged in');
etc...
See more info about flashdata here: https://ellislab.com/codeigniter/user-guide/libraries/sessions.html
Upvotes: 1