Sessions

They store whatever info you give them only for that browsing session: once you close the browser, your 'session' is over and all session information is gone.

On any page where you plan to use session variables, you start by calling:

session_start();

I should note that even if you just want to read (and not write) session variables on a given page, you still have to call that function.

<?php
session_start();
if (isset($_POST["name"])) {
  $_SESSION["name"] = $_POST["name"];
  ?>
  <p>
    Thanks!
    <a href='continue.php'>Go here for your message</a>
  </p>
<?php } else if (isset($_SESSION["name"])) {
  echo "Hi {$_SESSION["name"]}!";
  echo "<a href='logout.php'> Logout </a>";
} else { ?>
  <form action="continue.php" method="POST">
    <p>
      Name:
      <input type="text" name="name" />
    </p>
    <input type="submit" value="submit"/>
  </form>
<?php } ?>
Demo of above code

Name:


comments powered by Disqus