If isset $_SESSION goto this page?

If isset $_SESSION goto this page?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

If isset $_SESSION Goto This Page?

You’ve built a login gatekeeper that enforces authentication on protected routes and redirects users to a specific destination upon success. The logic seems sound: check for a session flag, execute the login, then redirect based on a stored variable or a default setting. However, there is a specific edge case where this pattern breaks down gracefully—when an authenticated user accidentally navigates directly to /login.php.

Your current implementation checks $_SESSION['goAfterLogin'] immediately upon loading the page. If that key exists, it grabs the value and unsets the key. The problem arises because you are checking for a redirect destination before verifying if the user is actually logged in. When an authenticated user hits this endpoint, the script sees no goAfterLogin flag (since they are already inside the system), skips the redirect logic entirely, and proceeds to the login form or validation block.

To fix this, you need to decouple the "redirect destination" retrieval from the "authentication status" check. The priority order matters here:

  1. Check Authentication Status First: Determine if the user is already logged in before looking for a redirect target.
  2. Determine Destination Only If Needed: If the user is logged in, they should go to their dashboard or index page immediately. You do not need to look for $_SESSION['goAfterLogin'] because there is no "after login" event happening right now.
  3. Handle Logout/Non-Auth: Only if the user is not logged in, then you proceed to check for a stored redirect target (perhaps from a previous failed attempt or a specific URL parameter) and display the login form.

Here is how you can restructure the logic within inc/functions.class.php or your main controller:

require_once "inc/functions.class.php";
$quickprotect = new functions('inc/ini.php');

// 1. Check if the user is already authenticated first
$isLoggedIn = $quickprotect->is_logged_in();

if ($isLoggedIn) {
    // User is logged in, so we don't need to look for 'goAfterLogin' here.
    // Instead, redirect them to their default dashboard or index.
    header("Location: " . $quickprotect->settings['DEFAULT_LOGIN_SUCCESS_PAGE']);
    exit();
}

// 2. If not logged in, handle the login logic and redirection setup
if (isset($_POST['username'])) {
    // Attempt login
    if ($quickprotect->login($_POST['username'], $_POST['password'])) {
        // Successful login: store where they want to go for the next step
        $goto = $_SESSION['goAfterLogin'] ?? $quickprotect->settings['DEFAULT_LOGIN_SUCCESS_PAGE'];
        
        // Clear any previous stored destination since we are starting fresh
        unset($_SESSION['goAfterLogin']);
        
        header("Location: " . $goto);
    } else {
        // Login failed, but maybe we had a 'goAfterLogin' set for a previous attempt?
        // Or just go to the default login page.
        $goto = $_SESSION['goAfterLogin'] ?? $quickprotect->settings['DEFAULT_LOGIN_PAGE'];
        unset($_SESSION['goAfterLogin']);
        
        // Display error message here instead of redirecting immediately
    }
} else {
    // GET request, user is not logged in, show the form.
    // Note: If a 'goAfterLogin' was stored from a previous failed attempt, 
    // you might want to preserve it for when they eventually succeed.
    $goto = $_SESSION['goAfterLogin'] ?? $quickprotect->settings['DEFAULT_LOGIN_PAGE'];
}

The Trade-off of Session Storage

One common pitfall with the $_SESSION['goAfterLogin'] pattern is storing a redirect target even after a failed login attempt. If a user tries to log in as admin but fails, and then tries again, you might want them to stay on the login page, not jump back to the dashboard immediately upon success.

However, if you unset $goto too aggressively, you lose the context of where they wanted to go. A robust solution involves distinguishing between a "forced redirect" (e.g., from ?redirect=/admin/dashboard) and a "post-login default".

In your specific case, since is_logged_in() returns false for an unauthenticated user regardless of the session content (assuming the admin password check fails), you must ensure that the is_logged_in method is robust. Your source material shows:

if ($_SESSION['logged_in'] === md5($this->settings[ADMIN_PW])) {
    return true;
}

This relies on a static hash of the admin password stored in the session. If your application supports multiple users, this approach is inherently limited to an administrator-only session check. If $_SESSION['logged_in'] does not exist for a regular user, the comparison fails, returning false. This behavior is acceptable for a simple script but fragile for multi-user systems where user IDs should be stored in the session rather than a generic boolean string.

Why This Approach Works

By moving the authentication check to the very top, you ensure that an authenticated user never enters the login form logic at all. They bypass the $_POST check entirely and are redirected immediately to their default page. This prevents the "accidental stumble" scenario where a logged-in user sees a login form or gets stuck on the index page because the redirect variable wasn't set.

If you are building this on a larger scale, consider if storing a redirect URL in the session is necessary at all. For many applications, checking the REQUEST_URI against a list of public pages (like /login) and protecting everything else via middleware or route guards is cleaner than managing session-based redirects. Tools like Laravel Company often advocate for this separation of concerns, where authentication guards handle redirection automatically based on route requirements rather than manual session manipulation.

Our network