views:

1155

answers:

3

Are there any reasons not to want to use a multi dimensional SESSION array to store temporary user data?

A: 

None that i can think of.

If you are worried serialize it first into a string.

Question Mark
The array will be serialized anyway
soulmerge
No point - data is automatically serialized by PHP before being stored anyway.
Peter Bailey
+3  A: 

I'd say it depends more on the size of the data and not on the number of dimensions, because the data is serialized before storing. Of course, a deep multi-dimensional array may cause a performance hit too, but this is a common indicator that there's a better way to do it.

Ignas R
+1  A: 

Unless you're storing megabytes worth of data, it should make negligible performance difference how you choose to make use of the $_SESSION array, as it just gets serialized to a string. Personally, I'm a fan of creating a Session class and saving an instance of it in $_SESSION['session']. Something like this:

<?php
class Session
{
    private $something;

    public function Session()
    {
        // Constructory things
    }

    // Methods to your heart's content
}

if (session_id() == '')
{
    session_start();
}

if (empty($_SESSION['session']))
{
    $_SESSION['session'] = new Session();
}
$session =& $_SESSION['session'];
?>

Save that in a file called session.php, and then just require 'session.php' at the top of every php file where you need access to the session, and access the session through the $session variable defined at the bottom.

Skinniest Man
thanks for the tip.looks like it can be useful
chris