If isset $_SESSION goto this page?

72,426

You don't mention how you store your users in your session, but something like this should do it for you:

if(isset($_SESSION['user']))
{
    header("Location: index2.php");
    exit;
}

This will check if you have a user in your session, and if so, redirect to index2.php.

You need to change 'user' according to your session key.

Share:
72,426
eberswine
Author by

eberswine

Updated on July 05, 2022

Comments

  • eberswine
    eberswine about 2 years

    Ok, having trouble here:

    I created a login script, so after a person logs in then they will get direted to another page. And also, I have it redirecting them to the login page if they try and access one of those other pages.

    My problem is, if a user is logged in and stumbles to the login page again --by accident-- I would like for it to recognize that the user is logged in and redirect them to that next page (which is index2.php) ?? Having troubles :-(

    Here is my code so far:

    require_once "inc/functions.class.php";
    $quickprotect = new functions('inc/ini.php');
    
    if (isset($_SESSION['goAfterLogin'])){
        $goto = $_SESSION['goAfterLogin'];
        unset($_SESSION['goAfterLogin']);
    }
    else $goto = $quickprotect->settings['DEFAULT_LOGIN_SUCCESS_PAGE'];
    
    if (isset($_POST[username])) {
        if($quickprotect->login($_POST[username], $_POST[password])) header ("Location: $goto");
    }
    

    Here is how I store a users session in the functions page

     public function is_logged_in() {
            //Determines if a user is logged in or not. Returns true or false;
                if ($_SESSION['logged_in'] === md5($this->settings[ADMIN_PW])) {
                    return true;
                }
                else return false;
            }
    
  • eberswine
    eberswine over 13 years
    Perfect!! I had a function named logged_in a had to replace it with!! Great stuff.