EchoFool Posted December 14, 2013 Share Posted December 14, 2013 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!! Link to comment https://forums.phpfreaks.com/topic/284767-php-session-keeps-setting-the-data-to-null/ Share on other sites More sharing options...
EchoFool Posted December 14, 2013 Author 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. Link to comment https://forums.phpfreaks.com/topic/284767-php-session-keeps-setting-the-data-to-null/#findComment-1462345 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.