开发者

Session variable in Kohana 3 keeps being reset after having been deleted

开发者 https://www.devze.com 2023-04-02 12:22 出处:网络
I have a Notices class which is used to provide one-time message notifications. The notifications are stored in a Kohana session (cookie) and my getter looks like this (echos are there for debugging p

I have a Notices class which is used to provide one-time message notifications. The notifications are stored in a Kohana session (cookie) and my getter looks like this (echos are there for debugging purposes):

private static function _get($key)
{
    $session = Session::instance();
    $value = $session->get($key);

    echo $key . ': ' . $session->get($key) . '<br />';

    $session->delete($key);

    echo 'deleted ' .$key. ', value now: ' .$session->get($key). '<br /><br />';

    return $value;
}

Now, one page I use this class on is the login page. When a user enters incorrect login info it does what I expect it too: displays an error saying "username/password incorrect". This message should then be deleted as I've called the getter and the getter deletes it from the session.

However, this is not the case. The message is shown on the next page too. And if I go to any other page which displays notifications, the error is shown there too, until a different error is displayed (say if I enter information on another form incorrectly).

This is what is displayed from the echo's on the page:

error: Username/password combination is inco开发者_StackOverflow中文版rrect.

deleted error, value now:

Which suggests that the value is being deleted, but it's still showing the same thing when I refresh the page.

If it helps this is what the process of setting an error message is like, from a controller I call Notices::error('message'); which calls this method:

public static function error($value = NULL)
{
    return Notices::_notice('error', $value);
}

which calls:

private static function _notice($key, $value)
{
    // If value is not NULL, then store the notice
    if ( ! is_null($value))
    {
        Notices::_set($key, $value);
        return TRUE;
    }
    else
    {
        // If value is not NULL. retieve key and format HTML
        if ( ! is_null($value = Notices::_get($key)))
        {
            return "$value";
        }
        // If the key does not exist return empty string
        else
        {
            return '';
        }
    }
}

and the setter looks like this:

private static function _set($key, $value)
{
    Session::instance()->set($key, $value);
    return TRUE;
}


If session settings are playing up on you, check two things:

  1. Your browser cache is not so aggressive that it appears to hold on to remnants of elements unless you force flush it.
  2. You're not also setting the session in the same method, thus having it re-fill the variable when you refresh the page or call the method again.

You should also check out the existing get_once method in the Kohana_Session class that destroys the variable after it's done taking it out of the $_SESSION variable:

/**
 * Get and delete a variable from the session array.
 *
 *     $bar = $session->get_once('bar');
 *
 * @param   string  variable name
 * @param   mixed   default value to return
 * @return  mixed
 */
public function get_once($key, $default = NULL)
{
    $value = $this->get($key, $default);

    unset($this->_data[$key]);

    return $value;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号