...

View Full Version : Can we pass object through sessions



chleng
09-12-2005, 11:49 AM
Hi

in processingtransfer13.php

session_start();
session_register('old_balance');
session_register('new_balance');

$old_balance = new Balance;
$old_balance->RetreiveBalances($retrieved_phone_number);
$old_balance->UpdateUserBalances($retrieved_phone_number, $credit_entered);

$new_balance = new Balance;

$new_balance->RetreiveBalances($retrieved_phone_number);

in transfer_process_results04.php

<?=$old_balance->mysmsbalance;?>;//but did not display anything, could anybody tell me why

thks in advance

rgds

missing-score
09-12-2005, 11:55 AM
Firstly, session_register() is deprecated, you should use:



$_SESSION = $varname;


Secondly, in order to send objects and arrays through the session you should use serialize(), then unserialize() after being passed... eg:



<?php
// Page1.php:
session_start();
$var = new object_name;
// Do whatever...
$_SESSION['var'] = serialize($var);


// Page2.php
session_start();
$var = unserialize($_SESSION['var']);
?>

chleng
09-20-2005, 03:39 AM
Hi,

how do I do a check in 2nd page?

session_start();

if ( isset(_$SESSION['new_balance']))
$new_balance= unserialize(_$SESSION['new_balance']);

OR

$unserialize_new_balance = unserialize(_$SESSION['new_balance']);

if ( isset($unserialize_new_balance))
{

}

else
{
exit();
}


2nd question

once unserialize, can we still unserialize again

for example,

the moment I press F5, compiler will complain that

unserialize(_$SESSION['new_balance'])

_$SESSION['new_balance'] is not a string

but since documentation for unserialize is unserialize (string str) and I did not do like this,

str = unserialized(str), why then I got this error

thks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum