Dynamically Start Session? [Solved]
Posted: Mon Jul 04, 2011 11:59 pm
Rather than calling session_start() on each page, or setting the .ini file to auto start sessions, I want to start a session only when needed. I have a custom session handler and the constructor calls session_start().
The session handler is loaded into a registry, so I can start the session handler (which starts the session) by doing something like:
$registry->session_handler();
The problem is, this is no different than calling session_start(), and I don't need a session for every page of my site...
To get around this, I want to mimic the old session_register() function that is now deprecated, which implicitly calls session_start(). In short, I'd like to do something like:
$registry->session_handler()->set_var(); or $registry->session_handler()->get_var();
This is where I've painted myself into a corner... It's very easy to manage data in the $_SESSION super global array, but $_SESSION doesn't exist until session_start() is called. I also havent found a way to easily manage session data using the set/get functions. I was planning to use a path structure and build nested array keys based on the path (ex: $_SESSION['foo']['bar']['baz']).
This is what I've been working on, but I feel like it has a smell about it. I could use some guidance and/or thoughts. (please)
Edit: Made a little progress with get_var()
The session handler is loaded into a registry, so I can start the session handler (which starts the session) by doing something like:
$registry->session_handler();
The problem is, this is no different than calling session_start(), and I don't need a session for every page of my site...
To get around this, I want to mimic the old session_register() function that is now deprecated, which implicitly calls session_start(). In short, I'd like to do something like:
$registry->session_handler()->set_var(); or $registry->session_handler()->get_var();
This is where I've painted myself into a corner... It's very easy to manage data in the $_SESSION super global array, but $_SESSION doesn't exist until session_start() is called. I also havent found a way to easily manage session data using the set/get functions. I was planning to use a path structure and build nested array keys based on the path (ex: $_SESSION['foo']['bar']['baz']).
This is what I've been working on, but I feel like it has a smell about it. I could use some guidance and/or thoughts. (please)
Code: Select all
<?php declare(encoding = 'UTF-8');
class session_handler
{
/* ... */
function set_var($path, $value = '')
{
if(is_string($path) && !is_null($value)) {
$path = explode('/', dirname($path . '/.'));
foreach($path as $key => $piece) {
if(empty($piece) || !preg_match('/^[a-z][a-z0-9]*$/ui', $piece))
unset($path[$key]);
}
print_r($path);
/* Output:
Array
(
[1] => asd
[6] => authorized
[12] => asds
)
*/
}
}
}
$registry->session_handler()->set_var('/asd///../../authorized//////asds//.', 'test');
?>Edit: Made a little progress with get_var()
Code: Select all
function get_var($path)
{
if(is_string($path)) {
$path = explode(DS, dirname($path . DS . '.'));
foreach($path as $key => $piece) {
if(empty($piece) || !preg_match('/^[a-z][a-z0-9_]*$/ui', $piece))
unset($path[$key]);
}
$path = array_values($path);
$input = array(&$_SESSION);
while(list($k, $v) = each($input)) {
foreach($v as $key => $val) {
if($key == $path[0]) {
if(!next($path))
return $input[$k][$key];
else
array_shift($path);
$input = array(&$input[$k][$key]);
}
}
}
# Throw Notice:
trigger_error("Undefined index: {$path[0]}", E_USER_NOTICE);
}
}
# Pulls value from $_SESSION['authentication']['last_impress']
$registry::session_handler()->get_var('/authentication/last_impress/'); // 1309845395