CodeIgniter Forums
Session is created everytime I refresh my browser. - 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 is created everytime I refresh my browser. (/showthread.php?tid=53085)



Session is created everytime I refresh my browser. - El Forum - 07-10-2012

[eluser]jpganz[/eluser]
Hi!

I am trying to use session on codeigniter but I am getting an error, everytime I refresh my page seems like CI creates another session, and of course I cant save my info.

Any idea why is this? how could I solve it?

My code is this

Code:
$this->load->library('session');
print_r  ($this->session->all_userdata());

And my configuration (because I read this could cause the problem) is this

Code:
$config['sess_cookie_name'] = 'site';
$config['sess_expiration']  = 3600;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = FALSE;
$config['sess_table_name']  = 'session';
$config['sess_match_ip']  = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;

/*
|--------------------------------------------------------------------------
| Cookie Related Variables
|--------------------------------------------------------------------------
|
| 'cookie_prefix' = Set a prefix if you need to avoid collisions
| 'cookie_domain' = Set to .your-domain.com for site-wide cookies
| 'cookie_path'   =  Typically will be a forward slash
| 'cookie_secure' =  Cookies will only be set if a secure HTTPS connection exists.
|
*/
$config['cookie_prefix'] = "site";
$config['cookie_domain'] = "http://localhost/";
$config['cookie_path']  = "/";
$config['cookie_secure'] = FALSE;

Any help would be greatful appreciated.


Session is created everytime I refresh my browser. - El Forum - 07-11-2012

[eluser]LuckyFella73[/eluser]
Try this:
Code:
$config['cookie_prefix'] = "";
$config['cookie_domain'] = ($_SERVER['SERVER_NAME'] == 'localhost' ? '' : preg_replace('/^www\./', '', $_SERVER['SERVER_NAME']));
$config['cookie_path']  = "/";
$config['cookie_secure'] = FALSE;

I prefer to save session data in DB - youcould try that too. Be sure to
set up the table the right way. Older versions from user guide had some
issues (not enough chars for user agent)

Template for session table:
Code:
DROP TABLE IF EXISTS `ci_sessions`;

CREATE TABLE IF NOT EXISTS  `ci_sessions` (
  `session_id`    varchar(40)           DEFAULT '0' NOT NULL,
  `ip_address`    varchar(45)           DEFAULT '0' NOT NULL,
  `user_agent`    varchar(120)                      NOT NULL,
  `last_activity` int(10)      unsigned DEFAULT 0   NOT NULL,
  `user_data`     text,
  PRIMARY KEY (`session_id`),
  KEY `last_activity_idx` (`last_activity`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

EDIT: you should encrypt you session data btw