/    Sign up×
Community /Pin to ProfileBookmark

Error storing sessions in DB

Hi guys,

Just got a little confused about what is going on here. Tbh this is not something i have ever done before, and im a little lost. Below is the error im getting while trying to tell it to save the sessions in my DB.
[b]
Warning: session_set_save_handler() [function.session-set-save-handler]: Argument 1 is not a valid callback in /nfs/c01/h03/mnt/32719/domains/dating.ryanlund.co.uk/html/classes/session_class.php on line 6
[/b]

And here is the first part of the class(where i am assuming this problem is):

[code]
function __CONSTRUCT(){
$checker=new checker();//ignore this obviously
session_set_save_handler(‘_open’,’_close’,’_read’,’_write’,’_destroy’,’_clean’);
}
function _open() {
return true;
}
[/code]

Now as far as i was concerned the open function didnt actually need to do anything (DB connection is already set up) – and simply returning true would be fine.

Anyone got any ideas?

Ryan

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiMay 22.2009 — If you're using a class, you need to pass in an array instead containing the classname and the method:

[code=php]
session_set_save_handler(
array('Session', 'open'),
array('Session', 'close'),
array('Session', 'read'),
array('Session', 'write'),
array('Session', 'destroy'),
array('Session', 'clean')
);
[/code]


Also I believe the methods should be public.

Any reason your constructor is in all caps? Thats probably not going to work on a unix like system.
Copy linkTweet thisAlerts:
@NogDogMay 22.2009 — I believe you can do it from within the object by using [b]$this[/b] along with the array syntax:
[code=php]
<?php
class SessionHandler
{
private $db;

public function __construct(mysqli $db)
{
$this->db = $db;
session_set_save_handler(
array($this, 'open'),
array($this, 'close'),
array($this, 'read'),
array($this, 'write'),
array($this, 'destroy'),
array($this, 'gc')
);
session_start();
}

public function open()
{
echo "Open";
}

public function close()
{
echo "Close";
}

public function read()
{
echo "Read";
}

public function write()
{
echo "Write";
}

public function destroy()
{
echo "Destroy";
}

public function gc()
{
echo "GC";
}
}

// EXAMPLE:
$db = new mysqli('localhost', 'xxxxx', 'xxxxx', 'test');
$sess = new SessionHandler($db);
$_SESSION['test'] = "This is a test.";
session_write_close();
echo "The end";
[/code]
Copy linkTweet thisAlerts:
@ryanlundauthorMay 22.2009 — ive always had it in caps and has never had a problem with any systems ive used lol....just habit really no particular reason.

Thanks for the advice tho, i wasnt aware of that =s ....will give it a try now ?

Ryan
Copy linkTweet thisAlerts:
@ryanlundauthorMay 22.2009 — Seems to have work, but in doing this i have given myself the same problem i created while using the file system where it wasnt staying logged in....back to the drawing board.
Copy linkTweet thisAlerts:
@ryanlundauthorMay 22.2009 — Right im sorry about this guys im being a real pain with this. Basically it isnt writing the ID and DATA to the datbase....and im really not sure why...below is the function, any clue's why it isnt doing this?

<i>
</i>function _write($id, $data){
$checker=new checker();
$id=$checker-&gt;cleanInput($id);
$access = time();
$data=$checker-&gt;cleanInput($data);
return mysql_query("REPLACE INTO <span><code>sessions</code></span> VALUES ('".$id."','".$access."','".$data."')");
}


*********edit**********

i have a plan....bare with me

*********************
Copy linkTweet thisAlerts:
@ryanlundauthorMay 22.2009 — Right that didnt work....open to idea's ?
Copy linkTweet thisAlerts:
@ryanlundauthorMay 22.2009 — Right, everything is sorted and its working perfectly, thankyou guys for helping me out with that =) (btw if anyone wants to know what that last problem was it was a problem outside the _write function - with my cleanInput() function)
×

Success!

Help @ryanlund spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...