CHECK PHP SESSION - ISSET($SESSION - IS NOT WORKING

CHECK PHP SESSION - ISSET($SESSION - IS NOT WORKING

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

Stefan Bogdanescu

Founder & Senior Architect

CHECK PHP SESSION - ISSET($SESSION - IS NOT WORKING

The core issue preventing your session validation from working isn't a logic error in your if statement; it is a fundamental breakage in how you are managing sessions in modern PHP versions. You are attempting to use legacy functions that have been removed or deprecated, specifically session_register(), which renders your entire authentication flow incompatible with current PHP environments (5.4+ and all of 7.x/8.x).

Your protection logic relies on checking if $_SESSION["myusername"] exists. In a healthy environment, this check works perfectly fine once a valid session has been established and data stored within it. However, your login script uses session_register("myusername"). This function was deprecated in PHP 5.4 and completely removed in PHP 7.0. When you run this code on a modern server, the session variable is likely never set at all because the old mechanism fails silently or throws an error that halts execution before data storage occurs. Consequently, the isset() check on your protected page returns false, redirecting users incorrectly or failing to recognize valid logins.

To fix this, you must migrate from the deprecated registration method to the modern session array approach. The modern standard involves initializing a session using session_start() and then manually assigning values to the $_SESSION superglobal array. Here is how your login logic should look:

<?php
ob_start();
// ... database connection code (recommended to switch to PDO) ...

$myusername = $_POST['myusername']; 
$mypassword = $_POST['mypassword']; 

// Note: Using prepared statements is mandatory for security
$stmt = $pdo->prepare("SELECT * FROM members WHERE username=? AND password=?");
$stmt->execute([$myusername, hash_verify($mypassword)]); // Assuming password hashing

if ($stmt->rowCount() == 1) {
    // Start a session if one isn't already active
    if (session_status() === PHP_SESSION_NONE) {
        session_start();
    }

    // Store data directly in the array
    $_SESSION['myusername'] = $myusername; 
    // Storing password is bad practice. Just store an ID or flag.
    
    header("location:login_success.php");
} else {
    echo "Wrong Username or Password";
}
ob_end_flush();
?>

On the protected page, your original check remains valid, but it must be paired with session_start() to ensure the session is active before reading from it.

<?php
session_start(); // Crucial: Must happen before any output

if (!isset($_SESSION['myusername'])) {
    header("location:main_login.html");
    exit; // Stop processing
}
?>

There are critical security considerations embedded in this migration that you must address immediately. Your current login script uses the mysql_* functions, which have been removed from PHP core entirely. These extensions were notoriously insecure and prone to SQL injection attacks. You should switch to PDO or MySQLi with prepared statements. Furthermore, storing raw passwords in your session is a significant security flaw; if an attacker steals a session cookie, they gain access to the password. Instead of storing the username and password string, store the user's unique ID (user_id) or a specific role flag (e.g., $_SESSION['role'] = 'member'). This ensures that even if a session is hijacked, the attacker cannot easily pivot to other accounts without knowing the specific ID mapping.

Finally, ensure your cookies are configured correctly. Session hijacking often occurs not because of code logic errors but because cookies are sent without the HttpOnly flag. Without this flag, JavaScript on the page can read the session cookie via document.cookie, allowing XSS attacks to steal active sessions. Set your cookie configuration in the login script to include session.cookie_httponly = 1;.

By updating your session storage mechanism and securing your cookie settings, you will resolve the "isset not working" issue and significantly harden your application against common web vulnerabilities.

Our network