I'm having trouble with some session management code.

What I'm after is a basic class which is capable of storing some data via
the built-in
session handling routines. The name of the session variable must be
configurable at
run-time.

i.e:

$object1 = new Object( 'object1_session_variable_name' );
$object2 = new Object( 'object2_session_variable_name' );

Unfortunately, I can't get this to work...

[ ---------- END CODE SAMPLE ---------- ]

class Test
{ 
var $sessvar; 

function Test( $sessvar ) 
{ 
// set the name of the session variable to use for persistent storage 

$this->sessvar = $sessvar; 
} 

function Execute( ) 
{ 
// get this instance's session data 

$container = &$this->container(); 

// display the data 

echo "container['sequence'] = " . $container['sequence'] . "<br>"; 
echo "container['signature'] = " . $container['signature'] . "<br>"; 

// update the sequence # value 

$container['sequence']++; 
} 

function &factory( $sessvar ) 
{ 
static $instances; 

// generate the instance id 

$id = $sessvar; 

// if the instance already exists, return it 

if (isset($instances[$id])) 
{ 
return $instances[$id]; 
} 

// create a new instance; 

$object = new Test($sessvar); 

// get the new instance's session data 

$container = &$object->container(); 

// set it's signature 

$container['siganture'] = $sessvar; 

// store the newly created instance 

$instances[$id] = &$object; 

// and return the new instance 

return $object; 
} 

function &container( ) 
{ 
static $container; 

if (!isset($container)) 
{ 
if (session_is_registered($this->sessvar)) 
{ 
if (ini_get("register_globals")) 
{ 
$container = &$GLOBALS[$this->sessvar]; 
} 
else if (function_exists("version_compare")) 
{ 
$container = &$_SESSION[$this->sessvar]; 
} 
else 
{ 
$container = &$HTTP_SESSION_VARS[$this->sessvar]; 
} 
} 
else 
{ 
session_register($this->sessvar); 

$container = array 
( 
'sequence' => 1, 
'signature' =>
 ""
);
}
}

return $container;
}
}

session_start();

$test1 = &Test::factory("test1_data");
$test2 = &Test::factory("test2_data");

echo "\$test1->Execute() reports: ";
$test1->Execute();

echo "\$test2->Execute() reports: ";
$test2->Execute();

echo "\$test1->Execute() reports: ";
$test1->Execute();

[ ---------- END CODE SAMPLE ---------- ]

Running this in a browser, I would expect to see the following:

$test1->Execute reports:
container['sequence'] = 1
container['signature'] = test1_data

$test2->Execute reports:
container['sequence'] = 1
container['signature'] = test2_data

$test1->Execute reports:
container['sequence'] = 2
container['signature'] = test1_data

And on refreshing the browser:

$test1->Execute reports:
container['sequence'] = 3
container['signature'] = test1_data

$test2->Execute reports:
container['sequence'] = 2
container['signature'] = test2_data

$test1->Execute reports:
container['sequence'] = 4
container['signature'] = test1_data

However... it doesn't!

Any clues, suggestions, hints, tips, etc. would be most gratefully recieved
as I'v been trying to
get this code to work for some considerable time now.

Oh and BTW, the code MUST be capable of running on PHP 4.0.0 but can take
advantage of anything
up-to and including PHP 4.1.0, and must use the standard built-in session
handling routines.

Thanks
--
Alan McFarlane
[EMAIL PROTECTED]
ICQ: 20787656



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to