-1
<?php 
session_start();

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Register Login and Logout User</title>
    <link rel="stylesheet" type="text/css" href="Style.css">
</head>
<body>

<div class="header">
    <h1> Register, Login and Logout user php mysql</h1>
</div>
<?php 

    if (isset($_SESSION['message'])){
        echo "<div id='error_msg'>" .$_SESSION['message']. "</div>";
        unset($_SESSION['message']);
    }

    ?>


<h1>Home</h1>
<div><h4>Welcome <?php echo $_SESSION ['username']; ?></h4></div>
<div><a href="Logout.php">Logout</a></div>

</body>
</html>

This is my code how can I fix it

Notice: Undefined index: username in C:\xampp\htdocs\authentication\Online Catering Reservation System.php on line 29

Naveed Ramzan
  • 3,565
  • 3
  • 25
  • 30

2 Answers2

2

Replace these lines

<h1>Home</h1>
<div><h4>Welcome <?php echo $_SESSION ['username']; ?></h4></div>
<div><a href="Logout.php">Logout</a></div>

with

<?php if(isset($_SESSION['username']) && !empty($_SESSION['username'])){ 
<h1>Home</h1>
<div><h4>Welcome <?php echo $_SESSION ['username']; ?></h4></div>
<div><a href="Logout.php">Logout</a></div>
<?php }else{ ?>
//add code to redirect to home page
<?php } ?>
Naveed Ramzan
  • 3,565
  • 3
  • 25
  • 30
1

You need to test the existence of the index first:

<?php 
if(isset($_SESSION['username'])
    echo $_SESSION ['username'];
?>
Nicolas Cami
  • 458
  • 1
  • 4
  • 13