simple login session php

simple login session php

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

Stefan Bogdanescu

Founder & Senior Architect

Why Your PHP Login Redirects to the Login Page Instead of the Secured Page

A common frustration in PHP development is when a user successfully enters credentials, yet the application immediately redirects them back to the login form instead of granting access. This specific scenario often stems from a subtle but critical mismatch between how legacy and modern database extensions handle queries.

Looking at the provided loginproc.php code, the immediate issue lies in the use of the deprecated mysql_* functions. The script initializes a session correctly with session_start(), retrieves input via $_POST, and attempts to query the database. However, it uses mysql_query() and mysql_num_rows(). These functions are part of the original MySQL extension, which has been removed from PHP 7 and is deprecated in earlier versions due to security vulnerabilities and lack of support for prepared statements.

The code includes a file named ../../model/database.php, which establishes a PDO connection. However, the login logic does not utilize this connection object. Instead, it relies on the abandoned mysql_* functions directly. When you run mysql_query() in a modern environment, or if the underlying configuration is strict about using only PDO (as hinted by the existence of database.php), the query might fail silently or return false.

Crucially, mysql_num_rows() expects a valid resource representing a result set. If the query fails—due to deprecated function usage, SQL syntax errors, or connection issues—the variable $login will not be a valid object. Consequently, mysql_num_rows($login) will likely return 0. This triggers the else block, executing header('Location: index.php'), which forces the user back to the login page regardless of whether the credentials were actually correct.

To fix this, the application must unify its data access layer. The code already defines a PDO connection in database.php. You should refactor loginproc.php to use that same $db object and switch to prepared statements. Prepared statements prevent SQL injection by separating SQL logic from user input, which is a mandatory practice for any production system handling authentication.

Here is how the corrected login logic should look using the existing PDO setup:

<?php
require_once '../../model/database.php';

// Retrieve username and password from database using prepared statement
$stmt = $db->prepare("SELECT id FROM user WHERE username = :username AND password = :password");
$stmt->execute([
    ':username' => $_POST['username'],
    ':password' => $_POST['password']
]);

// Check if a matching row exists
if ($stmt->rowCount() > 0) {
    // Set username session variable
    $_SESSION['username'] = $_POST['username'];
    // Jump to secured page
    header('Location: securedpage.php');
    exit;
} else {
    // Jump to login page
    header('Location: index.php');
    exit;
}

In this implementation, rowCount() is the modern PDO equivalent for checking if a query returned results. Notice the use of exit after every redirect. Without exit, PHP might continue executing subsequent code, potentially causing session errors or unexpected behavior in frameworks like Laravel.

For developers looking to move away from procedural PHP entirely, migrating to a framework like Laravel offers built-in handling for sessions and authentication. If you are maintaining legacy code but want to ensure security, the transition to PDO with prepared statements is non-negotiable. For more guidance on modernizing PHP applications or integrating robust session management, resources like https://laravelcompany.com can provide further context on structured development practices.

Always ensure that your database credentials in database.php are valid and that the sports_db database actually exists. A silent connection failure would also result in an empty result set, leading to the same redirect loop observed in the original issue.

Our network