'The session is not working properly in PHP

The session is not working properly in PHP. The login page is working fine. But after redirecting to my home page I couldn't able to see my login details it throws this errorNotice : Undefined index: username in C:\xampp\htdocs\final\header.php on line

and if I use the if-else state to hide my login/signup button it's not working. So please someone help me with this issue

My Login page

<link rel="stylesheet" href="css/auth.css">
<body>

    <div class="login-box">   <h2>Login</h2>   <form action="handleLogin.php" method="post">
<div class="user-box">
  <input type="text" name="loginEmail" required="">
  <label>Email</label>
</div>
<div class="user-box">
  <input type="password" name="loginPass" required="">
  <label>Password</label>
</div>
<a href="#">
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <input type="submit" value="LogIn" name="submit">
</a><br>


<a class="nav_btn" href="signup.php"><i class="icon_profile"></i>Create New Account</a>   </form> </div>
</body> </html>

LoginHandle page

    <div class="login-box">   <h2>Login</h2>   <form action="handleLogin.php" method="post">
<div class="user-box">
  <input type="text" name="loginEmail" required="">
  <label>Email</label>
</div>
<div class="user-box">
  <input type="password" name="loginPass" required="">
  <label>Password</label>
</div>
<a href="#">
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <input type="submit" value="LogIn" name="submit">
</a><br>


<a class="nav_btn" href="signup.php"><i class="icon_profile"></i>Create New Account</a>   </form> </div>
</body> </html>

My header session

  echo ' <form class="form-inline my-2 my-lg-0" >
      <p class="text-light my-0 mx-2">Welcome'.$_SESSION["username"].' </p> 
</form>';


Solution 1:[1]

I think you should use the post method for the form and then store the values in the sessions but remember to start session first Here you logged in

<php? 
  // Check to see if we have some POST data, if we do process it
  if ( isset($_POST['name']) && isset($_POST['pass']) ) {
    $_SESSION["name"] = htmlentities($_POST["email"]);
     header( 'Location: view.php' ) ;
     return;
  }
?>


<form action="handleLogin.php" method="post">
  <div class="user-box">
    <input type="text" name="loginEmail" required="">
    <label>Email</label>
  </div>
  <div class="user-box">
    <input type="password" pass="loginPass" required="">
    <label>Password</label>
  </div>
  <input type="submit" value="LogIn" name="submit">
</form>

Solution 2:[2]

Did you actually defined $_SESSION['username'] = 'some username or database value'; any where at your back-end program? You should share that too if the problem has not been resolved!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lule Moses
Solution 2 Apollos Geofrey