Reply
session set save handler with object
Old 05-28-2008, 07:02 PM session set save handler with object
NullPointer's Avatar
Will Code for Food

Posts: 482
Name: Matt
Location: Irvine, CA
I've been using database sessions in a project I'm working on. For anyone who doesn't know how to do this documentation is as follows:
http://us3.php.net/manual/en/functio...ve-handler.php

The session_set_save_handler function allows you to specify how you want to handle sessions, you just have to write your own open, close, read, write, destroy and gc (garbage collection) functions. I've been able to accomplish this with functions but it came off as sloppy to me so I decided to build it all into an object.

The problem is now I'm not able to properly pass the handles of each function to the save_handler function. My code is as follows:
PHP Code:
class Session
{
     
//session handlers
    //This code is not relivant to my problem
}

ini_set('session.save_handler''user');

$dbSession = new Session();

session_set_save_handler(array(&$dbSession'open'),
                         array(&
$dbSession'close'), 
                         array(&
$dbSession'read'), 
                         array(&
$dbSession'write'), 
                         array(&
$dbSession'destroy'), 
                         array(&
$dbSession'gc')); //Line 88


session_start(); 
I get this error and warning:
Code:
Warning: session_set_save_handler() [function.session-set-save-handler]: Argument 1 is not a valid callback in session.php on line 85

Fatal error: session_start() [<a href='function.session-start'>function.session-start</a>]: Failed to initialize storage module: user (path: ) in session.php on line 88
I've also tried using session_set_save_handler in the following ways:
PHP Code:
//without passing by reference
session_set_save_handler(array(&$dbSession'open'),
                         array(
$dbSession'close'), 
                         array(
$dbSession'read'), 
                         array(
$dbSession'write'), 
                         array(
$dbSession'destroy'), 
                         array(
$dbSession'gc'));

//just strings
session_set_save_handler('dbSession->open''dbSession->close''dbSession-read''dbSession->write''dbSession->destroy''dbSession->gc');

//Using the object name rather than a reference to an instance of that object
session_set_save_handler(array(Session'open'),
                         array(
Session'close'), 
                         array(
Session'read'), 
                         array(
Session'write'), 
                         array(
Session'destroy'), 
                         array(
Session'gc')); 
I've seen a lot of "working" examples of this that people have posted but none seem to be working for me. Does anyone see a problem in my approach?
__________________
http://tinsology.com/ - Under construction
NullPointer is online now
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 05-28-2008, 08:06 PM Re: session set save handler with object
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 955
Name: Jeremy Miller
Location: Reno, NV
Did you try the format of Type 3 in the example on the php site? That would dump the ampersand in your arrays.
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-28-2008, 08:07 PM Re: session set save handler with object
NullPointer's Avatar
Will Code for Food

Posts: 482
Name: Matt
Location: Irvine, CA
yeah I did, no dice.
__________________
http://tinsology.com/ - Under construction
NullPointer is online now
Reply With Quote
View Public Profile
 
Old 05-28-2008, 08:08 PM Re: session set save handler with object
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 955
Name: Jeremy Miller
Location: Reno, NV
As a side note, I usually just write a few functions not in a class and then use

PHP Code:
session_set_save_handler('_session_open','_session_close','_session_read','_session_write','_session_destroy','_session_clean'); 
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-28-2008, 08:10 PM Re: session set save handler with object
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 955
Name: Jeremy Miller
Location: Reno, NV
I just took your code and dumped it into a file with session stubs:

PHP Code:
<?php
class Session
{
     
//session handlers
    //This code is not relivant to my problem
    
public function open() {
    }
    public function 
close() {
    }
    public function 
read() {
    }
    public function 
write() {
    }
    public function 
destroy() {
    }
    public function 
gc() {
    }
}

ini_set('session.save_handler''user');

$dbSession = new Session();

session_set_save_handler(array($dbSession'open'),
                         array(
$dbSession'close'),
                         array(
$dbSession'read'),
                         array(
$dbSession'write'),
                         array(
$dbSession'destroy'),
                         array(
$dbSession'gc'));


session_start();
?>
And, no compile errors. I'm running PHP 5.2.6
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-28-2008, 09:09 PM Re: session set save handler with object
NullPointer's Avatar
Will Code for Food

Posts: 482
Name: Matt
Location: Irvine, CA
I figured it out. Turns out that the code that I thought was irrelivant to my problem, was my problem. Thanks for the suggestions. I would give you some TP but apparently I need to spread it around some more, but next time I get I chance I will.

In case anyone is interested here is my Session class code, it might be a little bugged as I have not tested it much:
PHP Code:
class Session
{
    private 
$dbh;
    
    public function 
open()
    {
        
$dbuser 'removed';
        
$dbpass 'removed';
        
$persistent true;
        
        try
        {
            if(
$persistent)
            {
                
$this->dbh = new PDO('mysql:host=removed;dbname=removed'$dbuser$dbpass,array(
                                
PDO::ATTR_PERSISTENT => true
                                
));
            }
            else
            {
                
$this->dbh = new PDO('mysql:host=removed;dbname=removed'$dbuser$dbpass);
            }
        }
        catch (
PDOException $e)
        {
            return 
false;
        }
        
        return 
true;
    }
    
    public function 
close()
    {
        return 
true;
    }
    
    public function 
read($id)
    {
        
$stmt $this->dbh->prepare('SELECT `data` FROM `session` WHERE `id` = ?');
        
$stmt->bindParam(1$id);
        
$stmt->execute();
        
        if(
$row $stmt->fetch(PDO::FETCH_NUM))
        {
            return 
$row[2];
        }
        
        return 
'';
    }
    
    public function 
write($id$data)
    {
        
$stmt $this->dbh->prepare('REPLACE INTO `session` VALUES(?, ?, ?)');
        
$stmt->bindParam(1$id);
        
$stmt->bindParam(2time() + 60 60 24);
                      
$stmt->bindParam(3$data);
        return 
$stmt->execute();
    }
    
    public function 
destroy($id)
    {
        
$stmt $this->dbh->prepare('DELETE FROM `session` WHERE `id` = ?');
        
$stmt->bindParam(1$id);
        return 
$stmt->execute();
    }
    
    public function 
gc($max)
    {
        
$stmt $this->dbh->prepare('DELETE FROM `session` WHERE `expire` < ?');
        
$stmt->bindParam(1time());
        return 
$stmt->execute();
    }
}

ini_set('session.save_handler''user');

$dbSession = new Session();

session_set_save_handler(array(&$dbSession'open'),
                         array(&
$dbSession'close'), 
                         array(&
$dbSession'read'), 
                         array(&
$dbSession'write'), 
                         array(&
$dbSession'destroy'), 
                         array(&
$dbSession'gc'));


session_start(); 
__________________
http://tinsology.com/ - Under construction

Last edited by NullPointer : 05-28-2008 at 09:15 PM.
NullPointer is online now
Reply With Quote
View Public Profile
 
Old 05-28-2008, 09:30 PM Re: session set save handler with object
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 955
Name: Jeremy Miller
Location: Reno, NV
I have a question. I see you inserted the ampersands again in front of $dbSession, but your code is clearly for PHP 5, so shouldn't those entities be passed by reference by default?
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-28-2008, 10:30 PM Re: session set save handler with object
NullPointer's Avatar
Will Code for Food

Posts: 482
Name: Matt
Location: Irvine, CA
That was my latest attempt at solving the problem so it is still in the current build of my code, it works either way so I didn't notice it was still there.
__________________
http://tinsology.com/ - Under construction
NullPointer is online now
Reply With Quote
View Public Profile
 
Reply     « Reply to session set save handler with object
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.15785 seconds with 13 queries