How can I ensure that the name of the logged-in user is displayed instead of the login form on the homepage in PHP?

To display the name of the logged-in user instead of the login form on the homepage in PHP, you can check if the user is logged in by verifying their session. If the user is logged in, retrieve their name from the session and display it on the homepage. If the user is not logged in, display the login form as usual.

<?php
session_start();

if(isset($_SESSION['user_name'])) {
    $user_name = $_SESSION['user_name'];
    echo "Welcome, $user_name!";
} else {
    // Display login form
    echo "<form action='login.php' method='post'>
            <input type='text' name='username' placeholder='Username'>
            <input type='password' name='password' placeholder='Password'>
            <button type='submit'>Login</button>
          </form>";
}
?>