简体   繁体   中英

PHP Session doesn't work on another page using an array

So I made a registration page and i'm also using sessions to flash messages. The session is called 'flash' and I made an abstraction class for sessions to make it easier.

This is my Session class:

public static function exists($name)
{
    return (isset($_SESSION[$name]) ? (true) : (false));
}

public static function set($name, $value)
{
    return $_SESSION[$name] = $value;
}

public static function delete($name)
{
    if(self::exists($name)) {
        unset($_SESSION[$name]);
    }
}

public static function get($name)
{
    if(self::exists($name)) {
        return $_SESSION[$name];
    }
    return '';
}

public static function hasFlash()
{
    return (Session::exists('flash') ? (true) : (false));
}

public static function addFlash($message)
{
    if(Session::exists('flash'))
    {
        $msgArray = (Array) Session::get('flash');
        array_push($msgArray, $message);
        Session::set('flash', (Array) $msgArray);
    }
    else
    {
        $msgArray = array();
        array_push($msgArray, $message);
        Session::set('flash', (Array) $msgArray);
    }
}

public static function flash($message = null)
{
    if(self::hasFlash()) {
        $msgArray = (Array) Session::get('flash');
        Session::delete('flash');
        foreach($msgArray as $message) {
            echo $message . "<br>";
        }
    }
    return '';
}

And this is my registration page:

$hasError = false;
        $username = Functions::escape(Input::get('user'));
        $password = Functions::escape(Input::get('password'));
        $email = Functions::escape(Input::get('email'));
        $nick = Functions::escape(Input::get('nick'));

        if(User::userExists($username, $db)) {
            Session::addFlash("User $username is taken, try another one.");
            $hasError = true;
        }

        if(User::emailExists($email, $db)) {
            Session::addFlash("Email $email is taken, try another one.");
            $hasError = true;
        }

        if(!$hasError)
        {
            User::addUser($username, $email, $password, $nick, $db);
            Session::addFlash("You have successfully registered.");
            Session::addFlash("You can now login with $username.");
            Functions::location("index.php");
        }

That's the code i'm using to display the flash messages:

<?php if(Session::hasFlash()) : ?>
            <div class="form-group">
                <?php Session::flash(); ?>
            </div>
        <?php endif; ?>

However it only works on the registration page for instance when a user types in a username/email that is taken, the code above will show the message though when I register the user and send him to the index page, the 2 success messages won't show up. I do have session_start at the top of the page.

The issues seemed to resolve by adding the following:

// server should keep session data for AT LEAST 1 hour
ini_set('session.gc_maxlifetime', 3600);

// each client should remember their session id for EXACTLY 1 hour
session_set_cookie_params(3600);

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