My flash data messages are not getting passed after a redirect in Codeigniter. I have a User controller:
class User extends CI_Controller {
public function register(){
$data['title']='Register Here';
$this->form_validation->set_rules('name' , 'Name' , 'trim|required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('user_name' , 'Username' , 'trim|required|max_length[32]|callback_check_username_exists'); // or is_unique[users.username]
$this->form_validation->set_rules('password', 'Password','trim|required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('confirm_password', 'Confirm Password','trim|required|min_length[5]|max_length[12]|matches[password]');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]', array('is_unique'=>'Email Already Used'));
if ($this->form_validation->run()==FALSE){
$this->load->view('templates/header');
$this->load->view('users/register',$data);
$this->load->view('templates/footer');
} else {
$option=array('cost'=>12);
$encrypt_password=password_hash($this->input->post('password'),PASSWORD_BCRYPT,$option);
$this->user_model->add_user($encrypt_password);
$this->session->set_flashdata('user_registered','You are Successfully Registered and Logged In');
// This works print_r($this->session->flashdata('user_registered'));
redirect('posts');
}
}
In the User controller flashdata 'user_registered ' is getting stored when I echo it as in comment above .
My POST controller is :
class Posts extends CI_Controller {
public function index(){
$data['title']='Latest Posts';
$posts=$this->post_model->get_post();
$data['posts']=$posts;
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
My header view is :
<html>
<head>
<title>My Blog </title>
<link rel="stylesheet" href="<?php echo base_url();?>asset/css/bootstrap.min.css">
<link rel="stylesheet" href="<?php echo base_url();?>asset/css/style.css">
<script src="https://cdn.ckeditor.com/4.11.1/standard/ckeditor.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="<?php echo base_url();?>">Bloggy</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor02" aria-controls="navbarColor02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarColor02">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="<?php echo base_url();?>">Home </a>
</li>
<li class="nav-item">
<a class="nav-link" href="<?php echo base_url();?>posts">Blog</a>
</li>
<li class="nav-item">
<a class="nav-link" href="<?php echo base_url();?>category">Categories</a>
</li>
<li class="nav-item">
<a class="nav-link" href="<?php echo base_url();?>about">About</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="nav-item">
<a class="nav-link" href="<?php echo base_url();?>user/register">Register</a>
</li>
<li class="nav-item">
<a class="nav-link" href="<?php echo base_url();?>posts/create">Create Post</a>
</li>
<li class="nav-item">
<a class="nav-link" href="<?php echo base_url();?>category/create">Create Category</a>
</li>
</ul>
</div>
</nav>
<div class="container">
<!-- Flash Data -->
<?php if($this->session->flashdata('user_registered')): ?>
<?php echo $this->session->flashdata('user_registered');?>
<?php else :?>
<?php echo ' no value '; ?>
<?php /*echo '<p class="alert alert-success">'.$this->session->flashdata('user_registered').'</p>'; */?>
<?php endif; ?>
In the header view the flashdata user_registered is showing up with no value. There is only one redirect and I should be able to access the flash data.
On further investigation I found my sessions are not working at all. I have auto loaded the sessions library and in the browser I am seeing ci_session as a cookie but I tested out both userdata and flashdata are not working after redirect. Flash data should work after for one redirect as I have been doing this for a long time. My previous application which worked perfectly is also failing because sessions are not getting stored.
I created a test code like below:
class User extends CI_Controller {
public function index (){
$sex='M';
$data1['user']=$this->user_model->get_all_users($sex);
$this->session->set_userdata('test','data');
$this->session->set_flashdata('flash', 'data');
print_r($_SESSION); exit();
redirect('projects');
}
I get Array ( [__ci_last_regenerate] => 1547034241 [test] => data [flash] => data [__ci_vars] => Array ( [flash] => new ) ) .
Now I echo the session in my projects controller :
<?php
class Projects extends CI_Controller {
public function index()
{
print_r($_SESSION); exit();
$result= $this->project->get_projects();
$data['result']=$result ;
$this->load->view('pages/projects',$data);
}
}
I just get this : Array ( [__ci_last_regenerate] => 1547034384 ) .
My config.php is $config['cookie_prefix'] = '';
$config['cookie_domain'] = '';
$config['cookie_path'] = '/';
$config['cookie_secure'] = FALSE;
$config['cookie_httponly'] = FALSE;