0

I'm trying to use session variable $_SESSION['name'] on the second page i.e review.php. but it gives me nothing on the second page.How to use Session variable on the second page? This is index.php

  <?php 
    session_start();
    $connect = mysqli_connect('localhost','root','root','review');
     if(isset($_POST['submit']))
     {
        $id = $_POST['id'];
        $_SESSION['name']  = $id;
     }



   ?>

<!DOCTYPE html>
<html>

  <head>
    <title>
      login
    </title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
  </head>

  <body>
    <form action="review.php">
      <input type="text" name="id" placeholder="id">
      <input type="password" name="password" placeholder="password">
      <input type="submit" name="submit" class="btn btn-success">
    </form>
  </body>

</html>

here is review.php

<?php 

session_start();
$connect = mysqli_connect('localhost','root','root','review');
if(isset($_SESSION['name']))
{
    echo $_SESSION['name']; 
 }
 else{
    echo "nothing";
 }

?>
Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
Khan
  • 23
  • 1
  • 9

1 Answers1

0

You have 2 problem:

  1. You send submitted form to review.php, so you should read form data in review.php
  2. You did not set form method. As default method is GET, but you read $_POST variable. (What is the default form HTTP method?)

So you should change index.php as follow:

<?php 
    session_start();
    $connect = mysqli_connect('localhost','root','root','review');
    if(isset($_POST['submit']))
    {
        $id = $_POST['id'];
        $_SESSION['name']  = $id;
    }
?>

<!DOCTYPE html>
<html>
<head>
    <title>
        login
    </title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
</head>
<body>
    <form method="post">
        <input type="text" name="id" placeholder="id">
        <input type="password" name="password" placeholder="password">
    <input type="submit" name="submit" class="btn btn-success">
</form>
</body>
</html>