I’ve been trying to set up saving sessions with session_set_save_handler() and i dont know what’s the problem, i have all my callable functions but sth is going wrong, here i post my code. btw i’m working in WP.
<?php
$root = dirname(dirname(dirname(dirname(__FILE__))));
if (file_exists($root.'/wp-load.php')) {
require_once($root.'/wp-load.php');
}
if (file_exists($root.'/wp-config.php')) {
require_once($root.'/wp-config.php');
}
global $wpdb;
//function called when session_start();
function _open() {
print "Session opened.n";
print "</br>";
return true;
}
//called at the end of the page
function _close() {
print "</br>";
print "Session closed.n";
print "</br>";
return true;
}
//function called after session_start();
function _read($session_id) {
global $wpdb;
$session_id = mysql_real_escape_string($session_id);
$record = $wpdb->get_var( "SELECT data FROM sessions_gm WHERE id='$session_id'");
print "SELECT data FROM sessions_gm WHERE id='$session_id'";
print "Session READ.n";
print "</br>";
print "El record:".$record;
if($record)
return $record;
return '';
}
function _write($session_id, $data) {
global $wpdb;
$access = time();
$session_id = mysql_real_escape_string($session_id);
$access = mysql_real_escape_string($access);
$data = mysql_real_escape_string($data);
$session = $wpdb->get_row("SELECT * FROM sessions_gm WHERE id = '$session_id'");
print "SELECT * FROM sessions_gm WHERE id = '$session_id'";
print "La session:";
print_r($session);
print "</br>";
print "Session value written.n";
print "</br>";
print "Sess_ID: $session_idn";
print "</br>";
print "Data: $datann";
print "</br>";
if($session == null)
{
$insert = $wpdb->insert(
'sessions_gm',
array(
'id' => $session_id,
'access' => $access,
'data' => $data
),
array(
'%s',
'%d',
'%s'
)
);
if($insert!=false)
{
print "Se inserto.";
return true;
}
}
else
{
$update = $wpdb->update(
'sessions_gm',
array(
'access' => $access, // string
'data' => $data // integer (number)
),
array( 'id' => $session_id ),
array(
'%d', // value1
'%s' // value2
),
array( '%s' )
);
if($update!=false)
{
print "Se updateo.";
return true;
}
}
return false;
}
// called when session_destroy();
function _destroy($session_id) {
global $wpdb;
$session_id = mysql_real_escape_string($session_id);
print "</br>";
print "Session destroy called.n";
print "</br>";
$delete=$wpdb->query( "DELETE FROM sessions_gm WHERE id = '$session_id'");
if($delete!=false)
{
print "Se elimino";
return true;
}
return false;
}
function _gc($maxlifetime) {
print "</br>";
print "Session garbage collection called.n";
print "</br>";
print "Sess_maxlifetime: $maxlifetimen";
print "</br>";
$old = time() - $max;
$old = mysql_real_escape_string($old);
$delete=$wpdb->query( "DELETE FROM sessions_gm WHERE access < '$old'");
if($delete!=false)
{
print "Se elimino las pasadas.";
return true;
}
return false;
}
var_dump(is_callable("_open"));
var_dump(is_callable("_close"));
var_dump(is_callable("_read"));
var_dump(is_callable("_write"));
var_dump(is_callable("_destroy"));
var_dump(is_callable("_gc"));
if (session_set_save_handler("_open", "_close", "_read", "_write", "_destroy", "_gc")) {
die('Works fine');
}
else {
die('Couldn't set session handler');
}
session_set_save_handler("_open", "_close", "_read", "_write", "_destroy", "_gc");
session_start();
$_SESSION['Username'] = 'clau';
print_r($_SESSION);?>
So for some reason in my if:
if (session_set_save_handler("_open", "_close", "_read", "_write", "_destroy", "_gc")) {
die('Works fine');
}
else {
die('Couldn't set session handler');}
it’s returning Couldnt set session handler.
Thanks for the help! 😀
Try add
session_write_close()
function before setting the handler:It looks like the session is already started. Then reinit:
first, this is wrong
Because when you call die(…); the
session_start();
don’t fired and session not start.Second, your approach is confused, try this example:
Ok , in this example i use Users class coded by me for users login, in this case when
fired it set users logged-out, but most important when you fire your logout write this:
Becouse if you do not delete all cookie this session remain alive.
This work fine for me, i hope this help you and excuse me for my english.