CodeIgniter Forums
Session creates new session id on every page load - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Session creates new session id on every page load (/showthread.php?tid=32254)

Pages: 1 2 3 4 5 6 7


Session creates new session id on every page load - El Forum - 07-17-2010

[eluser]jaystang[/eluser]
Hey Guys,
I'm having a hard time working with CodeIgniters session class. I was originally using the CI_Session w/o a database and I was noticing my session was getting lost from page to page. I then tried to switch to the native session library and I was having the same issue. Finally I created the ci_session db table and enabled 'sess_use_database'. Still not working for me, but I can see where the issue is a little easier. On every page load of the site I can see a new record w/ a different 'session_id' is created in the database. Even if I sit on the same page and just refresh I'm getting the same behavior. I would post code but honestly it happens w/o any code. I am autoloading the session class and that's it. I commented out all of my session setter and getter code and the problem still exists.

Anyone have any thoughts on this?

Thanks for the help!


Session creates new session id on every page load - El Forum - 07-18-2010

[eluser]WanWizard[/eluser]
Make sure the cookie domain and path are defined properly.

To make sure they're always in sync with the rest of the config, I use
Code:
// make sure the base_url is always defined properly
if(isset($_SERVER['HTTP_HOST']))
{
    $config['base_url'] = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
    $config['base_url'] .= '://'. $_SERVER['HTTP_HOST'];
    $config['base_url'] .= isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != '80' ? ( ':'.$_SERVER['SERVER_PORT'] ) : '';
    $config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
}
else
{
    $config['base_url'] = 'http://localhost/';
}

// determine the domain and the path from the base_url
$base_url_parts = parse_url($config['base_url']);
$config['cookie_domain']    = $base_url_parts['host'];
$config['cookie_path']        = $base_url_parts['path'];
unset($base_url_parts);



Session creates new session id on every page load - El Forum - 07-18-2010

[eluser]jaystang[/eluser]
Hi WanWizard,
Thanks for the response. I've tried your code and still nothing. Same result. I've also tried to hard-code all the paths and no luck there either.

Just to give you an idea of my environment the site's root is setup in the follow location "http://localhost/CUBEX/CMBProject/". My config is the following...



Code:
$config['base_url']    = "http://localhost/CUBEX/CMBProject/";

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

$config['cookie_prefix']    = "";
$config['cookie_domain']    = "localhost";
$config['cookie_path']        = "/CUBEX/CMBProject/";
//Note: I've also tried $config['cookie_path'] = "/";

Does this all look correct?


Session creates new session id on every page load - El Forum - 07-18-2010

[eluser]WanWizard[/eluser]
You are sure your browser isn't blocking cookies? What browser are you using?


Session creates new session id on every page load - El Forum - 07-18-2010

[eluser]jaystang[/eluser]
I'm developing with Firefox 3.6.6 but I've also tried IE8 and Chrome v5 and I get the same result.


Session creates new session id on every page load - El Forum - 07-18-2010

[eluser]jaystang[/eluser]
I seemed to fix the problem, I'm just a little confused how/why? I'm still using all the code you provided. I ended up created a VirtualHost in my httpd.conf file with a fake domain name and just adding it to my hosts file to point to 127.0.0.1 so instead of going through "http://localhost/CUBEX/CMBProject/" I'm just going through "http://www.cmbprojectdev.com".


Session creates new session id on every page load - El Forum - 07-18-2010

[eluser]WanWizard[/eluser]
Heard before that some people have trouble using 'localhost' as cookie domain.

The RFC states: domains must have at least two (2) or three (3) periods in them to prevent domains of the form: ".com", ".edu", and "va.us".

Strictly speaking, 'localhost' as domain is indeed not within the spec.


Session creates new session id on every page load - El Forum - 07-18-2010

[eluser]jaystang[/eluser]
Ah ok that makes sense. I'm ok running like this. Brings me closer to mimicking my production environment anyway. Thanks for the help!


Session creates new session id on every page load - El Forum - 07-25-2010

[eluser]benharrison[/eluser]
I am having this exact same problem. Does anyone have any other ideas?

I have already been running my dev environment as a vhost with a fake domain, and I followed the rest of the advice in this thread as closely as possible. However I am still getting a brand new session on each page load. I can't even get it to work with an actual domain in a staging environment.

If anyone has any other helpful advice, it would be very much appreciated. Thank you.


Session creates new session id on every page load - El Forum - 07-25-2010

[eluser]Prophet[/eluser]
[quote author="benharrison" date="1280125208"]I am having this exact same problem. Does anyone have any other ideas?

I have already been running my dev environment as a vhost with a fake domain, and I followed the rest of the advice in this thread as closely as possible. However I am still getting a brand new session on each page load. I can't even get it to work with an actual domain in a staging environment.

If anyone has any other helpful advice, it would be very much appreciated. Thank you.[/quote]

Apologies if you've done this already, but check that the sess_expiration and sess_time_to_update settings aren't too short.