Session class: Storing sessions in a database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Session class: Storing sessions in a database

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

That's ok :P
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Why are all your methods declared static?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

he's using static callbacks. :)
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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...
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Ah I see.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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. :?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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.
Post Reply