How can the code in the index file be modified to prevent automatic redirection to the login page without a username?

The issue can be solved by adding a condition in the index file to check if a username is set before redirecting to the login page. This can be done by checking if the session variable containing the username is empty or not. If it is empty, the user should be redirected to the login page.

<?php
session_start();

if(empty($_SESSION['username'])) {
    header("Location: login.php");
    exit;
}

// Rest of the index file code here
?>