0
votes

It seems something has gone wrong with my Codeigniter internal. I was able to access session variables from one page to another. But something made me to reinstall PHP and Database (MariaDB).

After the installation my Codeigniter project start losing session data from one page to another page. I did checked if session is actually creating in ci_sessions table; and behold it does; and i can access the session variable from the same page it was set with userdata and not flashdata.

For example

Tt.php

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

class Tt extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
    public function index()
    {
        $this->session->set_userdata('aa','emi');
        /* if header redirect is commented out and i echo the below
        echo $this->session->userdata('aa'); //will display "emi" */
        /*but if header redirect is not commented out and redirect to Vv.php controller*/
        header("Location: vv"); /*echo $this->session->userdata('aa'); shows nothing in Vv.php controller*/
    }
}

Vv.php

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

class Vv extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
    public function index()
    {
        echo $this->session->userdata('aa'); //shows nothing
    }
}

I haven't set anything else other than uninstalling and installation of php and database (MariaDB). But before then I can access session variable set in Tt in Vv, and vice versa.

Session setting in config.php is below:

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

Database Setting in database.php

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
  'dsn' => '',
  'hostname' => '127.0.0.1',
  'username' => 'root',
  'password' => '<mypassword>',
  'database' => 'appname',
  'dbdriver' => 'mysqli',
  'dbprefix' => '',
  'pconnect' => FALSE,
  'db_debug' => (ENVIRONMENT !== 'production'),
  'cache_on' => FALSE,
  'cachedir' => '',
  'char_set' => 'utf8',
  'dbcollat' => 'utf8_general_ci',
  'swap_pre' => '',
  'encrypt' => FALSE,
  'compress' => FALSE,
  'stricton' => FALSE,
  'failover' => array(),
  'save_queries' => TRUE
);

I've tried everything possible but still no luck to get it working back, I don't know if it is MariaDB lock on read. I will be glad if someone can suggest a solution.

1
and what appears when you do print_r($this->session->userdata); ?Sr. André Baill
print_r($this->session->userdata('aa')) displays "emi" in Tt.php, but when redirected to Vv.php controller print_r($this->session->userdata('aa')) displays nothingamachree tamunoemi
Load URL helper and try redirecting with redirect('vv', 'refresh') if working.Tpojka
still doesn't work.amachree tamunoemi
@amachreetamunoemi What version of CodeIgniter and PHP version are you using?MackieeE

1 Answers

0
votes

When you set you need to set session single item http://www.codeigniter.com/user_guide/libraries/sessions.html#adding-session-data

$data = array(
   'aa' => 'emi'
);

$this->session->set_userdata($data);

echo $this->session->userdata('aa');

Or you can group item

$data = array(
  'aa' => 'emi'
);

$this->session->set_userdata('group', $data);

Access group data like

$something = $this->session->userdata('group');

echo $something['aa'];

Auto load the session library

Also make sure you have set your session save path if your using CI3 in config.php http://www.codeigniter.com/user_guide/libraries/sessions.html#session-preferences