0
votes

I'm using Codeigniter to create a student testing system but for some reason my sessions data seems to be acting strangely. On my login page the user selects their username from a pulldown list (I doesn't have to be secure at this stage) this passes the userid of the selected user to a function which sets up the userdata in the session and then redirects them to the dashboard page. Now my problem is this. when the dashboard page is loaded either by using redirect in the url helper or manually by navigating to localhost/progsqs/dashboard, the session class creates a new, blank, session.

I wrote a few small test functions which do the same thing but they work fine one session with retrievable data (which rules out a codeigniter configuration problem) but for reasons unknown dashboard ALWAYS builds a new blank session and tries to work with it. I'm at my wits end and need a fresh pair of eyes to look at it and see the mistake I've obviously made but cannot for the life of me spot.

<?php
class Progsqs extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $this->login();
    }

    function login()
    {   
        $this->load->model('Progsq_Model');
        $data['users'] = $this->Progsq_Model->getUsers();
        $this->load->view('templates/header.php');
        $this->load->view('progsqs/login.php', $data);
        $this->load->view('templates/footer.php');
    }

    function logout()
    {
        $this->load->helper('url');
        $this->session->sess_destroy();
        redirect(base_url('progsqs/login'));
    }

    function setUser()
    {
        $this->load->helper('url');
        $this->load->model('Progsq_Model');
        $username = $this->Progsq_Model->getUsername($_POST['userlist']);
        $this->session->set_userdata('username', $username);
        echo $this->session->userdata('username');
        //redirect(base_url('progsqs/dashboard'));
    }

    function dashboard()
    {
        echo($this->session->userdata('username'));
        print_r($this->session->all_userdata());
    }
    function profile()
    {
        $this->load->view('templates/header');
        $this->load->view('progsqs/profile');
        $this->load->view('templates/footer');
    }

    function testBuilder()
    {

    }
    /*
        Debugging code for repairing session data
    */

    function inputtest()
    {
        $this->session->set_userdata('somedata', 'This is a test');
        $this->load->helper('url');
        redirect(base_url('progsqs/testoutput'));
    }

    function outputtest2()
    {
        echo($this->session->userdata('somedata'));
        //print_r($this->session->all_userdata());
    }

    function testoutput()
    {
        $this->load->model('Progsq_Model', '', TRUE);
        $data['users'] = $this->Progsq_Model->getUsers();
        $this->load->view('templates/header.php');
        $this->load->view('progsqs/outputtest.php', $data);
        $this->load->view('templates/footer.php');
    }

    function killsession()
    {
        $this->session->sess_destroy();
    }
}
?>
1

1 Answers

0
votes

You should check yout session config file.

an example :

$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = FALSE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;

If you're matching IP's have 0 in your sess_expiration

also make sure these aren't set :

 $config['cookie_prefix']    = "";
 $config['cookie_domain']    = "";

i had problems with them before with sessions.

These should be in your config file in your application folder "/application/config/config.php"

edit :

        function setUser()
{
    $this->session->set_userdata('username', 'alphabeta');
    header ('Location:' . base_url() . '/go/dashboard');
}

function dashboard()
{
    echo($this->session->userdata('username'));
    print_r($this->session->all_userdata());
}

I boiled down your two functions to this. I ran them on my server and it works perfectly.

if function setUser works fine, and you can actually echo out the stuff, and dashboard doesn't work(and creates a new session), I'm all out of ideas, sorry buddy