Page 1 of 1

Multiple session variables

Posted: Tue Jan 17, 2006 8:50 am
by hame22
Hi I need to create a number of session variables.

I can make them all like this

Code: Select all

$_SESSION['product_category'] = "var 1";
$_SESSION['page_count'] = "var 2";
$_SESSION['duration'] = "var3";
and so on.....

however is there an easier way to store these variables rather than write out lines of code, im interested to know?

thanks in advance

Posted: Tue Jan 17, 2006 11:07 am
by Chris Corbyn
:?

How do you mean? It depends where the variables are comming from. If they come from a single array then I ges you can yes.

You could cut down the number of characters you type by using references or functions too:

Function:

Code: Select all

function setVar($name, $value)
{
    $_SESSION[$name] = $value;
}

setVar('foo', 'bar');
echo $_SESSION['foo']; //bar
References:

Code: Select all

$x = &$_SESSION;

$x['foo'] = 'bar';

echo $_SESSION['foo']; //bar

Posted: Tue Jan 17, 2006 12:11 pm
by Jenk
setting session variables is just like setting any variable.. thus this is possibly as vague a question as you can get for php.