The _SESSION Misconception and How to Actually Access Session Data in PHP
The error message Notice: Undefined variable: _SESSION is a classic symptom of a fundamental misunderstanding regarding how PHP's session mechanism works, rather than a bug in the framework or a missing file. In your specific code snippet, line 9 attempts to access $_SESSION['SESS_fname'], but PHP throws this notice because you are treating $_SESSION as if it were a global variable that is automatically initialized and populated with data everywhere.
In reality, $_SESSION is a superglobal array, which means it is available in all scopes, but it is not pre-populated. It remains empty until the server explicitly starts the session engine. If you look at your file structure, you are including inc/incfiles/header.inc.php. This is likely where your application initializes the database connection and perhaps sets up some constants. However, unless that included file contains a call to session_start(), the session data will not be accessible anywhere in your script.
The Missing session_start() Call
The fix requires ensuring that session_start() is executed before any output is sent to the browser and before you attempt to read from or write to the $_SESSION superglobal. This function sends the appropriate HTTP headers to tell the client's browser to look for a session cookie and loads the contents of the session file into the memory space of your script.
If your header.inc.php does not contain this call, you have two options: either add it to that included file or move it to the very top of your main script before any HTML tags are printed. Given that you are using an include statement for headers, the cleanest architectural approach is usually to handle session initialization in a bootstrap file or the top of your layout header.
Here is what the corrected flow should look like:
<?php
// Ensure this runs before any output or include statements
session_start();
// Check if the user is actually logged in and has a name stored
if (!isset($_SESSION['SESS_fname'])) {
// Redirect to login or show a message if session data is missing
header("Location: login.php");
exit();
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Member Index</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Welcome <?php echo htmlspecialchars($_SESSION['SESS_fname']); ?></h1>
<a href="member-profile.php">My Profile</a> | <a href="logout.php">Logout</a>
<p>This is a password protected area only accessible to members.</p>
</body>
</html>
Security and Data Integrity Considerations
Beyond simply making the error disappear, there are two critical aspects of your implementation that need attention. First, the variable name SESS_fname suggests you might be manually constructing session keys. While valid, ensure that the session data is being stored securely on the server side. The value stored in $_SESSION['SESS_fname'] should ideally come from a database query executed after authentication, not just passed directly via URL parameters or POST data before the session starts.
Secondly, and most importantly, you must sanitize the output. Your current code echoes $_SESSION['SESS_fname'] directly. If a user's name contains quotes or script tags stored in your MySQL table, echoing it directly without escaping can lead to Cross-Site Scripting (XSS) vulnerabilities. Using htmlspecialchars() ensures that special characters are converted to HTML entities, rendering them harmless within the browser context.
Modernizing the Session Approach
While the session-based approach is standard and works perfectly for legacy PHP applications, many developers working with modern stacks might find themselves migrating away from raw sessions in favor of framework-specific solutions. If you are building a new application or refactoring an existing one, tools like Laravel offer robust session management out of the box, handling encryption, regeneration tokens, and CSRF protection automatically.
For instance, if you were porting this logic to a Laravel environment, you wouldn't manually call session_start(). The framework handles that in its service container, and accessing the user's name would look like $user->fname or auth()->user()->fname, abstracting away the underlying session array entirely.
However, if you are maintaining a traditional PHP application with XAMPP as shown in your file paths, sticking to the session_start() pattern is the correct path. The error you encountered was simply a missing initialization step. Once session_start() is called before accessing $_SESSION, the notice will vanish, and your welcome message will render correctly for authenticated users.
