CodeIgniter Forums

Full Version: n00b session problems
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]pdriessen[/eluser]
I made the follow test controller:
Code:
<?
class Test extends Controller {
    # START INDEX    
    function index()
        {
        $this->load->library('session');
        
        echo "</br>session_id: ";
        echo $this->session->userdata('session_id');
        
        echo "</br>test_key: ";
        echo $this->session->userdata('test_key');
        
        $this->session->set_userdata('test_key', 'test_value');
        
        echo "</br>session_id: ";
        echo $this->session->userdata('session_id');
        
        echo "</br>test_key: ";
        echo $this->session->userdata('test_key');
    } # EINDE INDEX
}
?&gt;
when I open this file I get this output:
Code:
session_id: 100c0ab06df21aeaa2e4b183ebb22fa8
test_key:
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/shoppeni/_si/system/application/controllers/beheer/test.php:10)

Filename: libraries/Session.php

Line Number: 662


session_id: 100c0ab06df21aeaa2e4b183ebb22fa8
test_key: test_value

The output and the error message are correct. The only problem is that when i refresh the page, I get a new session_id number and the previous set test_key had gone...

Anybody knows what I do wrong?

El Forum

[eluser]stommert[/eluser]
hi,

the first thing i noticed was a missing constructor with a reference to the controller class.
Code:
function __construct() {
parent::__construct();
}

but that was not the question.
Do you use a database session?
What does your config file look like?
You are aware of the fact that the session_id changes every x minutes?

I hope these pointers will help

El Forum

[eluser]pdriessen[/eluser]
I added de constructor...
I don't use a database session yet, this is my first try of sessions in CI. I am familiar with sessions in PHP.
I didn't autoload the session library. That's why I load it in the beginning of the function.

Normally when you create a session in PHP. The session_id stays the same as long as your around on a specific website. It is not usual that you get a new session_id every time you change from page.

El Forum

[eluser]stommert[/eluser]
hi,

check the 'how sessions work' section from this page.

it might have some thing to do with your cookie settings in your config file.

the way sessions work in CodeIgniter is different from the way they work in 'normal' php, for various reasons (security, scalability). That is why the session_id changes every 5 minutes (by default).

I hope you can make it work

El Forum

[eluser]pdriessen[/eluser]
oh... ok, thanks! That's why I said it was a n00b question. I did everything like in the 'how sessions work' section. But indeed I made a mistake in de config file. Thank you very much!