EchoFool Posted December 14, 2013 Share Posted December 14, 2013 (edited) Hello, I have a class which handles session data to my database but every time I run PHP and have session_start(), the session data in my database is set to 0. I have no idea why its doing it. My script is like this (sorry its a quite large class): class FileSessionHandler { private $_sess_db; function open($_sess_db, $sessionName) { $dsn = 'mysql:dbname=testDB;host=127.0.0.1'; $user = '[hidden]'; $password = '[hidden]'; try { $_sess_db = new PDO($dsn, $user, $password); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); exit; } $_sess_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->connection = $_sess_db; return true; } function close() { $this->connection = null; return true; } function write($id, $data) { $access = time(); $stmt = $this->connection->prepare("REPLACE INTO sessions (id,access,data) VALUES (?,?,?)"); try { $stmt->execute(array($id,$access,$data)); } catch (PDOException $e) { echo $e -> getMessage(); exit; } if(!$stmt->rowCount()){ return false; } return true; } function read($id) { $stmt = $this->connection->prepare("SELECT data FROM sessions WHERE id = ?"); try { $stmt->execute(array($id)); } catch (PDOException $e) { echo $e -> getMessage(); exit; } if(!$stmt->rowCount()){ return false; } $row = $stmt->fetch(); return $row['data']; } function destroy($id) { $stmt = $this->connection->prepare("DELETE FROM sessions WHERE id = ?"); try { $stmt->execute(array($id)); } catch (PDOException $e) { echo $e -> getMessage(); exit; } if(!$stmt->rowCount()){ return ''; } return 1; } function clean($max) { $old = time() - $max; $stmt = $this->connection->prepare("DELETE FROM sessions WHERE access < ?"); try { $stmt->execute(array($old)); } catch (PDOException $e) { echo $e -> getMessage(); exit; } if(!$stmt->rowCount()){ return ''; } return 1; } } $handler = new FileSessionHandler(); session_set_save_handler( array($handler, 'open'), array($handler, 'close'), array($handler, 'read'), array($handler, 'write'), array($handler, 'destroy'), array($handler, 'clean') ); session_start(); Now in my database the row has : id : 2f534c82c004172c4fd49cfdd8d5f91a access : 1387054887 data : 1 So the session is holding the value 1. Now when i run the PHP script and check the row, the field data says 0, i don't know why it keeps updating it to 0 rather than keeping the value? Please help been stuck on this all day!! Edited December 14, 2013 by EchoFool Quote Link to comment Share on other sites More sharing options...
Solution EchoFool Posted December 14, 2013 Author Solution Share Posted December 14, 2013 I have solved my own issue - for those who may want to know:The read function MUST return serialized session data (not php serialize) session has its own serialize. Otherwise it will simply be NULL if its not valid. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.