$_POST not working. "Notice: Undefined index: username..."

$_POST not working. "Notice: Undefined index: username..."

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

Stefan Bogdanescu

Founder & Senior Architect

Why $_POST Returns Undefined Index Despite Valid Form Submission

Seeing a "Notice: Undefined index: username" error when submitting a form is a classic beginner's headache, but it often stems from a subtle mismatch between how the browser sends data and how your server expects to receive it. The specific scenario described involves a login attempt where the HTML form correctly defines an input with name="username" and uses method="POST", yet the PHP script fails to detect the variable.

The most common culprit in these cases is not the POST mechanism itself, but rather the order of operations within your PHP logic or missing header directives. While the source material shows a functional form structure, the error message appears immediately upon line 11 of dologin/index.php. If you look closely at that file, the first thing the script does is execute database queries using $_POST['username'].

If the variable doesn't exist yet, PHP triggers the notice before any logic can handle it. However, a more frequent cause for this specific error pattern—especially when switching between GET and POST—is the absence of session_start() or handling cookies before accessing superglobals like $_GET or $_POST. In many web server configurations, specifically those running on Windows with XAMPP (as hinted by the file paths in the source), the default configuration for PHP sessions requires session_start() to be called at the very top of the script, before any output is sent.

If session_start() is missing or placed after an echo statement, it can cause headers to break, leading to unpredictable behavior when accessing $_POST. Furthermore, looking at the provided code snippet, there is a critical logical error: the script attempts to access $_GET['password'] inside the success block. If the form uses POST for both username and password, accessing $_GET will naturally return an undefined index if that field wasn't sent via GET. This suggests a confusion in variable naming or method usage within the logic itself.

Debugging the Superglobals

When you encounter "Undefined index", your first instinct should be to verify what is actually being sent. You can do this by adding a simple dump before the error occurs. Insert the following line at the very top of dologin/index.php:

<?php
var_dump($_POST);
var_dump($_GET);
// ... rest of code

This will instantly reveal whether the browser is actually sending the username key in the request body. If var_dump($_POST) returns an empty array, the form might be missing the name attribute in the HTML, or the form method isn't POST. However, if the data is there but the index is undefined, check for output buffering issues or previous file includes that might have sent a blank line.

The Session and Cookie Trap

The source code attempts to handle session cookies:

if(!isset($_COOKIE['PHPSESSID'])){
    session_start();
}

This logic is flawed. session_start() must be called unconditionally before any output, regardless of whether a cookie exists. Calling it inside an if block based on $_COOKIE is risky because accessing $_COOKIE requires the headers to be sent correctly first. If the previous line (the SQL query) generates a warning or notice, headers might already be compromised.

Furthermore, standard session handling relies on the server sending a Set-Cookie header. If your PHP version or server configuration disables cookie support by default, session_start() will fail silently or throw warnings, potentially affecting how superglobals are populated depending on the specific environment setup.

Modernization and Security Pitfalls

Beyond fixing the immediate error, the code relies heavily on deprecated functions. The mysql_* extension was removed in PHP 5.5.0 and is no longer supported. Using these functions will result in fatal errors on any modern PHP installation (8.x) or produce deprecation warnings in newer versions. You must migrate to MySQLi or PDO.

Additionally, the password hashing logic shown (md5("w@#$@#" + ...) is a severe security vulnerability. MD5 is cryptographically broken and should never be used for password storage. Never concatenate random strings into passwords before hashing them; this creates predictable collisions. Use password_hash() to create secure hashes and password_verify() for checking them.

If you are building complex applications where managing sessions, routing, and security manually feels overwhelming, consider leveraging a framework like Laravel. Laravel handles session management, CSRF protection, and database connectivity out of the box, abstracting away many of the low-level pitfalls that cause "undefined index" errors in raw PHP scripts.

Corrected Approach

Here is how the initialization should look to prevent these notices:

<?php
// 1. Start session FIRST, before any output or logic
session_start();

// 2. Check if data exists BEFORE using it
if (isset($_POST['username']) && isset($_POST['password'])) {
    $username = $_POST['username'];
    // Proceed with secure authentication logic here
} else {
    die("Form submission failed or missing fields");
}

// 3. Use PDO instead of deprecated mysql_* functions
// Example: $pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');

By ensuring session_start() is the first executable statement and validating that keys exist in $_POST before accessing them, you eliminate the "Undefined index" notice.

Our network