View Full Version : session_set_save_handler just ain't working out!
I am attempting to store session data in the database, but came accross some major drawbacks while using session_set_save_handler, most irritating is that it only seems to return the key, not the value. When I toggle commenting lines 1 and 2 of the next bit of code I find that the session never saves the value, but does save the key and if I remove line 1 I get an "Undefined variable: test2" error from line 3 even though the session is supposed to be saved??? :confused:
$test2='passed';
session_register('test2');
echo '<hr>'.$test2;
Before the code I also have the session functions defined followed by:
session_set_save_handler("sess_open","sess_close","sess_read","sess_write","sess_destroy","sess_gc");
session_start();
error_reporting(E_ALL);
mordred
10-20-2002, 02:04 PM
Just curious, which PHP version do you use?
Win2k, PHP 4.2.1 (sapi) on Apache 2.0.36
Why? Is there a known bug or something?
mordred
10-23-2002, 10:47 AM
It's just because you are using session_register, while you could be using $_SESSION somewhere else in your script. From php.net:
Caution
If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered() and session_unregister().
I experienced fuzzy behaviour when using both kinds of session registration in one script - don't know if that's actually related to your problem, but nevertheless, I just thought I'd mention that in case your PHP version is recent enough (which is).
Morded, I actually do use both registration methods (I started this project before there was the $_SESSION alias in PHP) but that did not seem to effect anything.
Okay, it was a stupid typeo:
should be:
return $dbresult;
I had:
$return = $dbresult;
Habitual actions really are an annoying flaw of being human... New issue though:
sess_gc() never seems to get called, any idea how often it is executed? In php.ini I have:
; Percentual probability that the 'garbage collection' process is started
; on every session initialization.
session.gc_probability = 1
Does that mean only 1 in 100 hits it will fire?
mordred
10-25-2002, 04:38 PM
Originally posted by Bawy
Morded, I actually do use both registration methods (I started this project before there was the $_SESSION alias in PHP) but that did not seem to effect anything.
If that's fine for you, okay. But you surely don't believe that the PHP guys added the mentioned passage just for fun. ;) When I have a bug I can't locate properly, I would not let my code run in a quirky mode, so to make sure that that's not the problem... since you found the error already, it doesn't matter anymore.
sess_gc() never seems to get called, any idea how often it is executed? In php.ini I have:
; Percentual probability that the 'garbage collection' process is started
; on every session initialization.
session.gc_probability = 1
Does that mean only 1 in 100 hits it will fire?
As I understand it, it means that gc will be done only on 1% of each session initialization. So it could be the 123rd hit, or the 95th. So basically yes.
Why do you need to change this value? Is your server cramped with old session data?
When I have a bug I can't locate properly, I would not let my code run in a quirky mode, Agreed, but combing through the code (or writing an app to do it) is not an option for deadline reasons.
Not exactly old data, but I wanted to test the gc and could never get it to fire. I suppose I could change the probability to 100 for testing...
mordred
10-25-2002, 08:54 PM
That is an option, but watch the performance of your server if that's going to be used in a production environment. Cleaning up the session directory of old data on each session request combined with a lot of hits takes huge resources of the server. But for testing purposes this should be neglectable... I suppose I didn't tell you anything unknown now. :)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.