Jump to content

Session in MySQL Database


micha8l

Recommended Posts

Hey guys,

 

I'm working a project that requires sessions be stored within the database, as the project I'm working on is on a shared host. But I'm having a problem with getting the data of a session in the database, the other fields like session_id, session_updated, session_created are working fine. I think I've got a bug in my code, but I just can't detect it (frustrating).

 

Database connection

class db extends mysqli {
private $host;
private $user;
private $pass;
private $db;

function __construct( $host='localhost', $user='user', $pass='pass', $db='website' ) {
	$this -> host = $host;
	$this -> user = $user;
	$this -> pass = $pass;
	$this -> db = $db;

	parent::connect( $host, $user, $pass, $db );

	if( mysqli_connect_error( ) ) {
		die( 'Connection error ('.mysqli_connect_errno(  ).'): '.mysqli_connect_error(  ) );
	}

}

function __destruct(  ) {
	$this -> close(  );
}
}

 

Session handler

class sessionHandler {
private $database;
private $dirName;
private $sessTable;
private $fieldArray;

    function sessionHandler() {
        // save directory name of current script
	$this -> database = new db;
        $this -> dirName = dirname(__file__);
        $this -> sessTable = 'sessions';
    }

    function open( $save_path, $session_name ) {
	return TRUE;
    }

    function close() {
	//close the session.
        if ( !empty( $this -> fieldarray ) ) {
            // perform garbage collection
            $result = $this->gc( ini_get ( 'session.gc_maxlifetime' ) );
            return $result;
        }
        return TRUE;
    }

    function read( $session_id ) {
	$sql = "
	SELECT *
	FROM sessions
	WHERE session_id=( '$session_id' )
	LIMIT 1
	";
	$result = $this -> database -> query( $sql );
	if( $result -> num_rows > 0  ) {
		$data = $result -> fetch_array( MYSQLI_ASSOC );
		$this -> fieldArray = $data;
		$result -> close();
		return $data;
	}
	return "";
    }

    function write( $session_id, $session_data ) {
	//write session data to the database.
        if ( !empty( $this -> fieldArray ) ) {
            if ( $this -> fieldArray['session_id'] != $session_id ) {
                // user is starting a new session with previous data
                $this -> fieldArray = array();
            }
        }

	$this -> fieldArray['session_id'] = $session_id;
	$this -> fieldArray['session_data'] = $session_data;
	$this -> fieldArray['session_updated'] = time();
	$this -> fieldArray['session_created'] = time();

	$session_id = $this -> database -> escape_string( $session_id );
	$session_data = $this -> database -> escape_string( $session_data );
	$session_updated = time();
	$session_created = time();	

	$sql = "
	INSERT INTO sessions ( session_id, session_data, session_updated, session_created )
	VALUES ( '$session_id', '$session_data', '$session_updated', '$session_created' )
	";
	if( $this -> database -> query( $sql ) !== TRUE ) {
		return FALSE;
	}
	return TRUE;
    }

    function destroy( $session_id ) {
	$sql = "
	DELETE FROM sessions
	WHERE session_id=('$session_id')
	";
	if( $this -> database -> query( $sql ) !== TRUE ) {
		return FALSE;
	}
	return TRUE;
    }

    function gc( $max_lifetime ) {
	return TRUE;
    }

    function __destruct() {
	//ensure session data is written out before classes are destroyed
	//(see http://bugs.php.net/bug.php?id=33772 for details)
	@session_write_close();
    }
}

 

The call

$session_class = new sessionHandler;
session_set_save_handler(
array( &$session_class, 'open' ),
array( &$session_class, 'close' ),
array( &$session_class, 'read' ),
array( &$session_class, 'write' ),
array( &$session_class, 'destroy' ),
array( &$session_class, 'gc' )
);
if( !session_start() ) {
exit();
}

 

Any help at all would be appreciated.

 

Kind Regards

Mike

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.