Saving State with Sessions Example

Starting a session registering two variables may look like page1.php below
<? 
 session_start();

 session_register("name","pass");

 if (session_is_registered("pass") && session_is_registered("name"))
     echo "<h1>name and password variables registered</h1>";

 $name = "hilde";
 $pass = "mypassword";

 echo "<h1>Session variables set!</h1>";
 echo "<a href=\"page2.php\">go to next page</a>";

?>

The next page, page2.php, might look like
<? 
 session_start();

 echo "<h1>The password of $name is $pass </h1>";

 session_unregister("pass");

 echo "<h2>password unregistered </h2>";

 echo "<a href=\"page3.php\">go to next page</a>";

?>
You may unregister all variables by calling session_unset();.

In the third page, page3.php, we do not have access to the variable pass anymore.
<? 
 session_start();
 echo "<h2>name is  $name </h2>";

 echo "<h2>password is  $pass </h2>";

 session_unregister("name");

 echo "Name unregistered, but name is set to $name until end of this script...";

 session_destroy();
 echo "<a href=\"page4.php\">go to next page</a>";

?>
page4.php
<? 
 session_start();
 echo "<h2>name is  $name </h2>";

 echo "<h2>password is  $pass </h2>";

 session_destroy();
 echo "<a href=\"page1.php\">go back to page 1</a>";
?>