HTML PHP Display username after success login

HTML PHP Display username after success login

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

Stefan Bogdanescu

Founder & Senior Architect

Fixing Session State in HTML: Why Your Login Logic Isn't Updating

You want your navigation menu to switch from a "Login/Register" link to a displayed username once a user successfully authenticates. The logic you've written uses PHP if/elseif blocks inside an HTML <li> element to decide what text to render based on the $_SESSION['logged'] flag. While this approach demonstrates an understanding of session variables, it fails because of how the PHP engine interacts with static HTML files and the specific behavior of your redirect logic.

The core issue lies in where you are executing the PHP code. Your file, onlinestore.html, is a static asset. When a browser requests an .html file, the server sends the raw source code to the client before any interpretation occurs, unless the server specifically configures that file as a PHP script. In your current setup, the browser likely treats onlinestore.html as pure text. Consequently, the PHP tags <?php ... ?> are not parsed by the server; they are sent directly to the browser. The user sees the literal string <?php session_start(); ... ?> in their source code or a parsing error, rather than the rendered HTML containing your username or link.

To make this work, onlinestore.html must be renamed to something like onlinestore.php. This simple extension change tells the web server (e.g., Apache or Nginx with PHP-FPM) that the file should be processed by the PHP interpreter. Once you rename it, the session_start() function will execute, and the if condition will correctly evaluate the session data before sending the HTML to the browser.

However, there is a secondary problem in your checklogin.php logic that complicates the user experience. You are using header("refresh:1;url=onlinestore.html") after a successful login. This performs a client-side redirect after a one-second delay. While functional, this introduces a race condition with session regeneration.

Modern web security best practices dictate that you should regenerate the session ID immediately after authentication to prevent session fixation attacks. If you do not call session_regenerate_id(true) in your checklogin.php before setting the flags, an attacker might predict the next session ID based on the old one. Furthermore, relying solely on a delayed redirect can sometimes cause issues if the user's connection drops during the wait time.

A more robust implementation handles the session state directly in the PHP file without relying on a temporary redirect delay for the state check. Here is how you should structure onlinestore.php:

<?php
session_start();

// Optional but recommended: Regenerate session ID after login to prevent fixation
if (isset($_SESSION['logged'])) {
    // Session already exists, potentially from a previous request or old code
} else {
    // Ensure we are using a fresh session for this request
    session_regenerate_id(false); 
}

// Determine the content based on session state
if (isset($_SESSION['logged']) && $_SESSION['logged'] === true) {
    $username = htmlspecialchars($_SESSION['username']);
?>
<li class="active" style="float:right;">
  <span><?php echo $username; ?></span>
  <a href="logout.php"><span>Logout</span></a>
</li>
<?php
} else {
?>
<li class="inactive" style="float:right;">
  <a href="registerform.html"><span>Login/Register</span></a>
</li>
<?php
}
?>

This approach ensures that the HTML output is generated dynamically based on the actual state of the session at the moment of request. By using htmlspecialchars() when echoing the username, you also prevent Cross-Site Scripting (XSS) vulnerabilities if a user tries to inject malicious JavaScript into their username field.

In your checklogin.php file, ensure that you are setting the session variables before performing any redirects. Additionally, consider removing the arbitrary refresh delays (refresh:1, refresh:2). Instead of forcing a client-side wait, rely on server-side logic or instant redirects to provide a snappier user experience. The server should handle the state change and immediately send the user back to the dashboard where the PHP logic will render the correct UI elements.

If you are looking for a more scalable architecture that handles these concerns out of the box, consider frameworks like Laravel, which manage session regeneration and routing automatically. You can find resources on implementing secure authentication flows at https://laravelcompany.com.

Our network