I have two websites: One is TLS and the other is not, both for the same client, but I need sites to share with each other (and only with each other) common data for users, orders, accounts and etc.
This is usually done with $_SESSION data, but I obviously cannot work on other sites, and I found that I can store session data in a database (MySQL), and not in the file system.
I dug up and found This is a useful guide , as well as this old but useful guide . I also found this guide that has slightly more modern MySQL.
I wrote an interface class, but it only partially works, it stores session data in the database, but does not retrieve it. I also used the suggested method from the PHP manual .
My MySQL (as copied from the first pair of links above):
CREATE TABLE `sessions` ( `id` varchar(32) COLLATE utf8_unicode_ci NOT NULL, `access` int(10) NOT NULL, `data` text COLLATE utf8_unicode_ci NOT NULL, UNIQUE KEY `id` (`id`) ) ENGINE=InnoDb DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Please note: Before I show you my interface class, please know that Db connetion uses my own user interface and works fine on its own.
$sessionDBconnectionUrl contains information about connecting to the session database, since I do sessions in a separate database from the main content of the website.
My interface class (as based on all of the above links)
<?php /*** * Created by PhpStorm. ***/ class HafSessionHandler implements SessionHandler { private $database = null; public function __construct($sessionDBconnectionUrl){ if(!empty($sessionDBconnectionUrl) && file_exists($_SERVER['DOCUMENT_ROOT'].$sessionDBconnectionUrl)) { require_once "class.dataBase.php"; // Instantiate new Database object $this->database = new Database($sessionDBconnectionUrl); } else { error_log("Session could not initialise class."); } } /** * Open */ public function open($savepath, $id){ $openRow = $this->database->getSelect("SELECT `data` FROM sessions WHERE id = ? LIMIT 1",$id); if($this->database->selectRowsFoundCounter() == 1){ // Return True return $openRow['data']; } else { // Return False return ' '; } /** * Read */ public function read($id) { // Set query $readRow = $this->database->getSelect('SELECT `data` FROM sessions WHERE id = ? LIMIT 1', $id,TRUE); if ($this->database->selectRowsFoundCounter() > 0) { return $readRow['data']; } else { error_log("could not read session id ".$id); return ''; } } /** * Write */ public function write($id, $data) { $access = time(); // Set query $dataReplace[0] = $id; $dataReplace[1] = $access; $dataReplace[2] = $data; if ($this->database->noReturnQuery('REPLACE INTO sessions(id,access,`data`) VALUES (?, ?, ?)', $dataReplace)) { return TRUE; } else { return FALSE; } } /** * Destroy */ public function destroy($id) { // Set query if ($this->database->noReturnQuery('DELETE * FROM sessions WHERE id = ? ', $id)) { return TRUE; } else { return FALSE; } } /** * Close */ public function close(){ // Close the database connection // If successful if($this->database->dbiLink->close){ // Return True return true; } // Return False return false; } /** * Garbage Collection */ public function gc($max) { // Calculate what is to be deemed old $old = time() - $max; // Set query if ($this->database->noReturnQuery('DELETE * FROM sessions WHERE access < ?', $old)) { return TRUE; } else { return FALSE; } } public function __destruct() { $this->close(); } }
My test page (written from scratch!)
<?php require "class.sessionHandler.inc.php"; $HSH = new HafSessionHandler("connection.session.dbxlink.php"); session_set_save_handler( $HSH, TRUE ); session_start(); print "<p>Hello this is an index page</p>"; $_SESSION['horses'] = "treesx3"; $_SESSION['tiespan'] = (int)$_SESSION['tiespan']+7; print "<p>There should be some session data in the database now. <a href='index3.php'>link</a></p>"; var_dump($_SESSION); exit;
Problem:
The executed test pages store the data in the database in order, but they don't seem to retrieve the data,
I have error logging enabled and no PHP errors are reported. No critical MySQL errors have been reported.
Why doesn't it work?