简体   繁体   中英

PHP - Multi dimensional SESSION array a good or bad idea for performance?

是否有任何理由不想使用多维SESSION数组来存储临时用户数据?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM