Page 1 of 1

Session class: Storing sessions in a database

Posted: Mon Aug 21, 2006 2:42 pm
by Oren
Ok, after several days I gave up and decided to post here for help.
I wrote a session class, something along these lines:

Code: Select all

<?php



	class session
	{
		private $db;



		public function __construct()
		{
			// Store a db object within $db
		}




		public static function _open()
		{
			return true;
		}




		public static function _close()
		{
			return true;
		}




		public static function _read($id)
		{
			// Read...
		}




		public static function _write($id, $data)
		{
			// Write...
		}




		public static function _destroy($id)
		{
			// Destroy...
		}




		public static function _clean($max)
		{
			// Clean...
		}
	}
Now I have exp.php with this code:

Code: Select all

<?php

		include 'my_session_class.php';

		$session = new session(); // <- In order to assign the db object into $db


		session_set_save_handler('session::_open',
					 'session::_close',
					 'session::_read',
					 'session::_write',
					 'session::_destroy',
					 'session::_clean');

		session_start(); // This is line 24 where the error occurs
The problem is that I keep getting this error:

Fatal error: session_start() [<a href='function.session-start'>function.session-start</a>]: Failed to initialize storage module: user (path: ) in C:\Apache2\htdocs\exp\exp.php on line 24

(The error I get looks exactly like the above)

Can someone shed some light on this?

P.S I modified the code and got rid from unnecessary parts, the logic is still the same though.

Thanks in advance guys :D

Posted: Mon Aug 21, 2006 2:48 pm
by feyd
Those, my friend, are not callbacks. :)

Have a look at the database sessions thread I linked to from Useful Posts for some contextual examples of callbacks.

Posted: Mon Aug 21, 2006 2:52 pm
by Luke
Not to toot my own horn, (because likely there are quite a few things wrong with my classes anyway :lol: ) but check out my session class here:
viewtopic.php?t=54059
I am doing pretty much what you are trying to do in that class.

Posted: Mon Aug 21, 2006 2:54 pm
by Oren
I did, but this is an old example for PHP 4 if I remember well and I'm new to OOP so this might not help me much.
I also thought to myself "Those are not callbacks.", so I changed the first argument to some undefined method and then I got an error telling me that the first argument supplied to session_set_save_handler() is not a valid callback. Therfore, I believed that after all, "Those are valid callbacks." :P

Posted: Mon Aug 21, 2006 2:58 pm
by Oren
Thanks The Ninja Space Goat, I already did as you can guess from reading my reply :P
I also read all your other posts related to this topic since I was working on a session class too in the last few days :D

Posted: Mon Aug 21, 2006 3:02 pm
by Luke
Oren wrote:Thanks The Ninja Space Goat, I already did as you can guess from reading my reply :P
:lol: sorry... didn't realize you had already posted in that thread haha.
<----- dork

Posted: Mon Aug 21, 2006 3:03 pm
by Oren
That's ok :P

Posted: Mon Aug 21, 2006 3:09 pm
by Benjamin
Why are all your methods declared static?

Posted: Mon Aug 21, 2006 3:13 pm
by feyd
he's using static callbacks. :)

Posted: Mon Aug 21, 2006 3:13 pm
by Oren
astions wrote:Why are all your methods declared static?
Otherwise I get an error: session_set_save_handler().... Argument 1 is not a valid callback in...

Posted: Mon Aug 21, 2006 3:29 pm
by Benjamin
Ah I see.

Posted: Mon Aug 21, 2006 4:09 pm
by Oren
Ok, I have just read a comment on http://www.php.net and in this comment the guy created a session class for PHP 5 which was similar to mine... and he used this code:

Code: Select all

session_set_save_handler(array(&$session, '_open'),
			 array(&$session, '_close'),
			 array(&$session, '_read'),
			 array(&$session, '_write'),
			 array(&$session, '_destroy'),
			 array(&$session, '_clean'));
This is the exact code from feyd's post... Eventually, this works with PHP 5 too :D
Before I mark this topic with [SOLVED], may I ask what's going on there?
Why are we passing arrays to session_set_save_handler()?
Why are we passing the object by reference? (In PHP 5, objects are passed by reference anyway)

Oh... and I changed my static functions to normal ones too :P

Posted: Mon Aug 21, 2006 4:17 pm
by Luke
Oren wrote:Ok, I have just read a comment on http://www.php.net and in this comment the guy created a session class for PHP 5 which was similar to mine... and he used this code:

Code: Select all

session_set_save_handler(array(&$session, '_open'),
			 array(&$session, '_close'),
			 array(&$session, '_read'),
			 array(&$session, '_write'),
			 array(&$session, '_destroy'),
			 array(&$session, '_clean'));
Kind of why I had thought you hadn't visited my thread... umm... that's what I did. :?

Posted: Mon Aug 21, 2006 4:21 pm
by Oren
The Ninja Space Goat wrote:Kind of why I had thought you hadn't visited my thread... umm... that's what I did. :?
That's kinda funny, the answer was lying under my nose for the last 2-3 days and I simply ignored it since I was sure this would work only with PHP 4.