Home > Enterprise >  The session is not working properly in PHP
The session is not working properly in PHP

Time:02-24

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 >   <h2>Login</h2>   <form action="handleLogin.php" method="post">
<div >
  <input type="text" name="loginEmail" required="">
  <label>Email</label>
</div>
<div >
  <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  href="signup.php"><i ></i>Create New Account</a>   </form> </div>
</body> </html>

LoginHandle page

    <div >   <h2>Login</h2>   <form action="handleLogin.php" method="post">
<div >
  <input type="text" name="loginEmail" required="">
  <label>Email</label>
</div>
<div >
  <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  href="signup.php"><i ></i>Create New Account</a>   </form> </div>
</body> </html>

My header session

  echo ' <form  >
      <p >Welcome'.$_SESSION["username"].' </p> 
</form>';

CodePudding user response:

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 >
    <input type="text" name="loginEmail" required="">
    <label>Email</label>
  </div>
  <div >
    <input type="password" pass="loginPass" required="">
    <label>Password</label>
  </div>
  <input type="submit" value="LogIn" name="submit">
</form>

  • Related