Ajax and PHP Sessions

474 views Asked by At

I have a template based site that relies on persistent sessions throughout. Recently I needed to expand the session storage by defining the save handler for database storage. This works fine in the general scope of the classes within the framework however when any of the pages make an ajax request the session id get regenerated and if the form gets submitted the previous session is gone.

  <?php

 require_once('site-database.php');
 require_once('site-config.php');


 class FileSessionHandler

 {


 private $database;

 private $life_time;


 public function FileSessionHandler(){


 $this->life_time = get_cfg_var("session.gc_maxlifetime");

 $this->database = new database();

 $this->database->newConnection(db_host,db_user_name,db_user_pass,db_user_database);


 session_set_save_handler(
array(&$this,'open'),
array(&$this,'close'),
array(&$this,'read'),
array(&$this,'write'),
array(&$this,'destroy'),
array(&$this,'gc')
);

}

2

There are 2 answers

0
Finn On BEST ANSWER

I altered a few variables within the php.ini file to see if something in there was causing an issue and now it works as well in the database as it did file based. Keeping the same ID.

session.cookie_secure was set to 1 in the .ini file and I changed that to 0.

0
AudioBubble On

Please check whether you have started the session with session_start().