Jump to content

PHP session keeps setting the data to NULL


EchoFool
Go to solution Solved by EchoFool,

Recommended Posts

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 by EchoFool
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.