PHP Cannot modify Header Warning [duplicate]

  php

I am getting the following error

Warning: Cannot modify header information – headers already sent by (output started at C:xampphtdocsdemosportfolioincludesheader.php:139) in C:xampphtdocsdemosportfoliologin.php on line 38

This warning occurs when I try to login into my website right after I click the login button. I look at both my header file and the login php page and I don’t understand why this warning is happening there no white space before the as well.

Is there a way to suppress this warning so the header method can refresh the page to hide the login form and display the friendly message that you’re login. I need to refresh the page so the navigation updates depending if you’re are login in are not and to stop the user from login in again until they logout. So Basically hide the form.

Everything work if I just manually refresh the page with the browser but it not a great user experience and I like to fix that.

Here my header.php

<?php
    include 'includes/session.php';
    include 'includes/function.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>P - <?php echo $pageTitle; ?></title>
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<style>
  .wrapper{
      width:80%;
      margin:0 auto;
  }
  .margin{
      margin-top: 25px;
      margin-bottom: 25px;
  }
  .mainBackground{
      background-color: #fc9c04;
  }
  .foreorange{
      color: #fc9c04 !important;
  }
  footer{
      position: fixed;
      left: 0;
      bottom: 0;
      height: 100px;
      width: 100%;
      /* overflow: hidden;  */
      padding-top: 20px;
      padding-bottom: 20px;
  }
  body {
      min-height: 400px;
      margin-bottom: 100px;
      clear: both;
  }
  .backgroundblack{
    background-color: #000000;
  }
  .textorange{
      color: #fc9c04;
  }
  .footerwidth{
      width: 30px;
      height: auto;
  }
  .dropdownwidth{
    width: 200px;
    margin-right: 50%;
  }
  .inputheader{

    width: 80% !important;
    padding: 0px !important;
    margin: 0px !important;
    box-sizing: none !important;

  }
  .buttonheader{
    width: 20% !important;
    height: auto !important;

  }
  .dropdowninputwidth{
    width: 125px;
  }
  .width{
      max-width: 15%;
      border-width:5px !important;
  }
  .textblack{
    color: #000000;
  }
  .dropdown-item{
    color: white;
  }
  .dropdown-item:active{
    color: white;
    background-color: #fc9c04;
  }
  .dropdown-item:hover{
    background-color: #fc9c04;
  }
  .buttonnav{
    color: white;
  }
</style>
<body class="mainBackground">
  <nav class="navbar-expand-lg navbar-light bg-light navbar navbar-dark bg-dark">
    <a href="/portfolio/index.php/" class="navbar-brand" href="#"><img src="/portfolio/includes/images/logo.PNG" class="img-fluid w-25 p-3" alt="Logo"></a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
  <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNav">
  <ul class="navbar-nav ml-auto">
    <li class="nav-item active">
      <a href="/portfolio/index.php/" class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
    </li>
    <li class="nav-item dropdown">  
      <a class="nav-link dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown">
        Blog
      </a>
    <div class="dropdown-menu dropdownwidth bg-dark" aria-labelledby="dropdownMenuLink">
      <a href="/portfolio/allBlogs.php/" class="dropdown-item" href="#">Blogs</a>
        <?php
          if(isset($_SESSION['username'])){
        ?>
          <a href="/portfolio/insertBlog.php/" class="dropdown-item" href="#">Insert Blog</a>
        <?php
            }
        ?>
        <?php
          if(isset($_SESSION['username'])){
        ?>
          <a href="/portfolio/editBlog.php/" class="dropdown-item" href="#">Edit Blog</a>
        <?php
            }
        ?>
        <form action="/portfolio/searchBlog.php/" class="dropdown-item" method="POST">
          <label for="searchBlog">Search Blog</label><br>
          <input type="text" class="dropdowninputwidth inputheader" id="searchBlog" name="searchBlog" placeholder="Search...">
          <button type="submit" class="bg-dark buttonnav buttonheader" name="SubmitBlog"><i class="fa fa-search "></i></button>
        </form>
    <?php
    if(isset($_POST["SubmitBlog"])){
    $_SESSION['blog'] =  $_POST['searchBlog'];
    }
    ?>
    </div>
    </li>
    <li class="nav-item dropdown">  
      <a class="nav-link dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown">
      Project
      </a>
        <div class="dropdown-menu dropdownwidth bg-dark" aria-labelledby="dropdownMenuLink">
          <a href="/portfolio/allProject.php/" class="dropdown-item"  href="#">Projects</a>
    <?php
    if(isset($_SESSION['username'])){
    ?>
          <a href="/portfolio/insertProject.php/" class="dropdown-item" href="#">Insert Project</a>
    <?php
      }
    ?>
    <?php
    if(isset($_SESSION['username'])){
    ?>
        <a href="/portfolio/editProject.php/" class="dropdown-item" href="#">Edit Project</a>
    <?php
      }
    ?>
    <form action="/portfolio/searchProject.php/" class="dropdown-item" method="POST">
      <label for="searchProject">Search Project</label><br>
      <input type="text" class="dropdowninputwidth inputheader" id="searchProject" name="searchProject" placeholder="Search...">
      <button type="submit" name="SubmitProject" class="bg-dark buttonnav buttonheader"><i class="fa fa-search"></i></button>
    </form>
    <?php
    if(isset($_POST["SubmitProject"])){
    $_SESSION['project'] =  $_POST['searchProject'];
    }
    ?>
    </div>
    </li>
    <?php
    if(isset($_SESSION['username'])){
    ?>
      <li class="nav-item dropdown">  
        <a class="nav-link dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown">
        Image
        </a>
        <div class="dropdown-menu dropdownwidth bg-dark" aria-labelledby="dropdownMenuLink">
    <?php
      if(isset($_SESSION['username'])){
    ?>
          <a href="/portfolio/uploadstarter.php/" class="dropdown-item" href="#">Upload Image</a>
    <?php
        }
    ?>
      <?php
      if(isset($_SESSION['username'])){
    ?>
          <a href="/portfolio/approveFile.php/" class="dropdown-item" href="#">Approve Image</a>
    <?php
        }
    ?>
    </div>
    </li>
    <?php 
      } 
    ?>
    <li class="nav-item">
    <a href="/portfolio/about.php/" class="nav-link" href="#">About</a>
    </li>
    <?php
    if(!isset($_SESSION['username'])){
    ?>
    <li class="nav-item">
    <a href="/portfolio/login.php/"  class="nav-link" href="#">Login</a>
    </li>
    <?php 
    }
    ?>
    <?php
    if(isset($_SESSION['username'])){
    ?>  
    <li class="nav-item">  
    <a href="/portfolio/logout.php/"  class="nav-link" href="#">Logout</a>
    </li>
    <?php 
    }
    ?>
    </ul>
  </div>
  </nav>
<div class="wrapper margin">

And here my login page where the header method is getting call when I login

<?php 
    $pageTitle = "Login";
    include 'includes/header.php';
    include 'includes/db/dbFunctions.php';
?>
<?php
    if(isset($_SESSION['username'])){
        echo "Thanks for logging in ", $_SESSION['username'];
        echo "<br/><a href='../../portfolio/allBlogs.php'>Goto All blogs</a>";
    //CHECK IF FORM HAS BEEN SUBMITTED
    }else if($_SERVER['REQUEST_METHOD'] == "POST"){
        //CHECK FOR EMPTY
        if(!empty($_POST['username']) && !empty($_POST['password'])){
            //store escaped $_POST values in variables
            $dbName = htmlentities($_POST['username']);
            $password = htmlentities($_POST['password']);
            $conn = connect('denismichaud_portfolio');
            $results = login($conn, $dbName, $password);
            if($results == null){
                echo "Invalid username and password";
            ?>
            <br/>
                <form action="login.php" method="post">
                    <div class="form-group">
                        <label for="username" class="font-weight-bold">Username</label>
                        <input type="text"  name="username" class="form-control" placeholder="Enter Username"  />
                    </div>
                    <div class="form-group">
                        <label for="password" class="font-weight-bold">Password</label>
                        <input type="text" name="password" class="form-control " placeholder="Enter Password"  />  
                    </div>
                    <input type="submit" class="textorange backgroundblack font-weight-bold" value="Login" />
                </form>
        <?php
            }else{
                $_SESSION['username'] = $dbName;
                    header('Refresh:0');      
                foreach($results as $row){
                    $id = $row['id'];
                }
                $_SESSION["id"] = $id;
            }
            $conn->close();
        }else{
            echo "Please fill out both fields";
        ?>
            <br/>
            <form action="login.php" method="post">
            <div class="form-group">
                <label for="username" class="font-weight-bold">Username</label>
                <input type="text"  name="username" class="form-control" placeholder="Enter Username"  />
            </div>
            <div class="form-group">
                <label for="password" class="font-weight-bold">Password</label>
                <input type="text" name="password" class="form-control " placeholder="Enter Password"  />  
            </div> 
            <input type="submit" class="textorange backgroundblack font-weight-bold" value="Login" />
        </form>
        <?php
        }
    //if the form wasnt submitted
    }else{
        if(isset($_GET['logout'])){
            if($_GET['logout'] == 1){
                echo 'You have been logged out';
                unset($_SESSION['id']);
            }
        }
        ?>
        <br/>
            <form action="login.php" method="post">
            <div class="form-group">
                <label for="username" class="font-weight-bold">Username</label>
                <input type="text"  name="username" class="form-control" placeholder="Enter Username"  />
            </div>
            <div class="form-group">
                <label for="password" class="font-weight-bold">Password</label>
                <input type="text" name="password" class="form-control " placeholder="Enter Password"  />  
            </div>
            <input type="submit" class="textorange backgroundblack font-weight-bold" value="Login" />
        </form>
    <?php
    }
    ?>
<?php
    include 'includes/footer.php'
?>

Source: Ask PHP

LEAVE A COMMENT