3

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.

1
  • You can try change encryption key in config.php file line number 317.you have to mention some string in the $config['encryption_key'] = 'your key';. its user defined Commented Mar 21, 2017 at 5:31

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Tks wolfgang, but i'm trying get session data from another function. In my case, i set a some data in session from index() function and i want to use this data in testsession(). Something like: stackoverflow.com/questions/26843009/…, It worked in my web server but not working in xampp (localhost)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.