0

Beginner PHP coder here

I am trying to use if statement to detect sessions, and by that changing the navigation bar from login to logout when logging in with the right username and password. I put the php script before the html but I still get the "Header already sent in" error. The Script:

    <?php
       session_start();
       ?>
       <DOCTYPE html>
       <html>
       <head>
       <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
       <meta name="viewport" content="width=device-width, initial-scale=1">
       <link rel="stylesheet" href=main.css>
       </head>                           
       <body>
       <div id="wrapper">
       <nav>
       <ul class="main_menu">
       <li><a href="mainpage.html">Main page</a>
       <li><a href="contact.html">Contact Us?</a>
       <li><a href="aboutus.html">About Us</a>
       <?php
       if (isset($_SESSION['username'])){

       echo "<li><a href="Logout.php">Logout</a>";

       } else {
       echo "<li><a href="Login.php">Login</a>";
       }

       ?>

       </ul>
       </nav>
       </div>

       </body>
       </html>

What am I doing wrong?

Coder48
  • 41
  • 6

1 Answers1

0

Make sure that you have no whitespace before PHP tag or anything you print before it all of these would make this problem

There is a fast solution but it not the best add at the top of your page before opening your session

ob_start(); // Output Buffering Start

and at the end of your script

ob_end_flush(); // Release The Output
Joseph
  • 5,644
  • 3
  • 18
  • 44