Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

None that i can think of.

If you are worried serialize it first into a string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top