Jump to content

[SOLVED] session_set_save_handler not saving session variables?


etabetapi

Recommended Posts

I am trying to debug my own session_set_save_handler when I noticed that it is not saving session variables beyond ones created on the same page. For example, if I create session variable to hold a string, I can output the value within the same script, but if I navigate to a new page which calls the same session variable it will output nothing.

 

I thought it was something to do with my code but I have stripped my session handler down to nothing (it literally should do nothing, unless I am seriously misunderstanding how this works) but whenever it is included in any of my scripts the same problem I described above happens. If I don't include it, my sessions behave normally. The session ids are the same so I don't know what's happening to cause the variable being lost?

 

<?php
#Filename "session_handler.php"
function __autoload($classname)
{
include $classname.".php"; //Here $classname=magicmethod1
}
function open(){
return true;
}
function close(){
return true;
}
function read(){
   return true;
}
function write(){
    return true;
}
function destroy(){
return true;
}
function gc(){
    return true;
}
session_set_save_handler('open','close','read','write','destroy','gc');
?>

 

The test scripts "page1.php" and "page2.php"

<?php
#Filename "page1.php"
include_once("session_handler.php");
session_start();
$_SESSION['test'] = "test";
echo "<p>session id ".session_id()."</p>";
echo "<p>value of test var on page1.php: ".$_SESSION['test']."</p>";
session_write_close();
//script should output "test"
?>

 

<?php
#Filename "page2.php"
include_once("session_handler.php");
session_start();
echo "<p>session id ".session_id()."</p>";
echo "<p>value of test var on page2.php: ".$_SESSION['test']."</p>";
//script should output "test" but outputs nothing
?>

I thought it was something to do with my code but I have stripped my session handler down to nothing (it literally should do nothing, unless I am seriously misunderstanding how this works) but whenever it is included in any of my scripts the same problem I described above happens.

 

Indeed, it does literally do nothing. None of your methods do anything except return true. How do you expect it to store sessions?

I thought it was something to do with my code but I have stripped my session handler down to nothing (it literally should do nothing, unless I am seriously misunderstanding how this works) but whenever it is included in any of my scripts the same problem I described above happens.

 

Indeed, it does literally do nothing. None of your methods do anything except return true. How do you expect it to store sessions?

 

I thought the session_set_save_handler only added functionality to whatever already existed to handle session variables? All the tutorials say you can use sessions normally. I don't want to override the existing functionality, I just want to add to it.

 

The reason I want to use the session_set_save_handler was because I wanted to store some variables in a database before a session is destroyed, but not all session data, just some. So I still want open, write, close, etc. to all do whatever they do normally but I want to write my own destroy function. Does that make sense?

Its called session_set_save_handler for good reason. You need to make your methods actually save the session data.

 

All the tutorials say you can use sessions normally

 

Once your handler is saving the sessions yes, you just use sessions as normal.

 

Have a look at the examples in the manual.

I have looked at the manual but I didn't know if that was the only way to do it. Like I said, I don't want to save all my data to a database, only some data, and only in certain circumstances (if the data exists, just before the session is destroyed.) How can I achieve this without using a database to store all my session data? The only way I can think of right now is to copy and paste the code for the default behavior into my session handler and append my code to it, but I'm not sure where to find this code. Do you know where it is located?

The only way I can think of right now is to copy and paste the code for the default behavior into my session handler and append my code to it, but I'm not sure where to find this code. Do you know where it is located?

 

Its built into php's core, written in C.

 

Have another look at example #1. It is basically the same as the native session handler.

Have another look at example #1. It is basically the same as the native session handler.

 

So for example #1 where it says

 

function open($save_path, $session_name)
{
  global $sess_save_path;

  $sess_save_path = $save_path;
  return(true);
}

 

Do I need to provide those arguments for open(), $save_path and $session_name, or are they automatically given the right values from some other function? Sorry, I don't know much about how sessions start or what the order of operations is.

Yes you need to define those arguments $save_path and $session_name are passed into the open function when session_start() is called.

 

You could pretty well copy that exact sample and use it. All you would then need do is modify the close function.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.