Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter session not working in another controller when using xampp

I created a Codeignier controller for testing, my controller using codeigniter session library:

Test.php

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

class Test extends CI_Controller {

     function __construct()
     {
       parent::__construct();
       $this->config->load('config');
       $this->load->helper("url");

     }


    function index()
    {
        $newdata = array(
            'username'  => 'uname',
            'email'     => '[email protected]'
            );

        $this->session->set_userdata('testsession', $newdata);
        redirect("https://mysite/index.php/test/getSession");
    }

    function getSession()
    {
        var_dump($this->session->userdata('testsession'));

    }

}

Session library is loaded in Codeigniter's autoload.

$autoload['libraries'] = array('session');

This code was working good in my web server with Apache + PHP 7.1 and MySQL, but not working with xampp 7.1.1 in Windows. Codeignitor sessions not work in getSession function when using xampp.

My Codeigniter config file is default and I checked PHP's TimeZone.

like image 268
GoDown Avatar asked Mar 22 '26 16:03

GoDown


1 Answers

You don't need to load the config/config.php file as it's all ready loaded.

Make sure you have set your sessions something like this I my self created a session folder in system so it is more protected. Set the folder permission 0700

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 1440;
$config['sess_save_path'] = BASEPATH . 'storage/session/'; 
$config['sess_match_ip'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = TRUE;

Note

EXT: The PHP file extension
FCPATH: Path to the front controller (this file) (root of CI)
SELF: The name of THIS file (index.php)
BASEPATH: Path to the system folder
APPPATH: The path to the "application" folder

Autoload.php

$autoload['libraries'] = array('session');

$autoload['helper'] = array('form', 'url');

Controller

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

class Test extends CI_Controller {

    function __construct() {
        parent::__construct();

        // Autoload the url helper in config/autoload.php
        $this->load->helper("url");
    }

    function index() {

        $newdata = array(
            'username' => 'uname',
            'email' => '[email protected]'
        );

        $this->session->set_userdata('testsession', $newdata);

        $session_data = $this->getSession();

        $data['username'] = $session_data['username'];

        $data['email'] = $session_data['email'];

        $this->load->view('test_view', $data);
    }

    function getSession()
    {
        return $this->session->userdata('testsession');

    }

}

Then on the views / test_view.php

<?php echo $username;?>

<?php echo $email;?>

Using the redirect() https://www.codeigniter.com/user_guide/helpers/url_helper.html#redirect


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!