PHP Sessions (Cont.)


Starting a PHP Session
After clicking on the button Check, the page start.html in the folder session will call the script 1.php. In 1.php, you must first start up the session before you can store user information in your PHP session. The session_start function must appear before the <html> tag.
session/start.html
1.php
 <?php
  unset( $_SESSION[customer] );
  session_start( );
  $_SESSION[customer] = $_POST[name];
 ?>

 <html><body>
 <?php
  echo "Page 1";
  echo "Name = " . $_SESSION[customer];
  echo "<form method='post' action='2.php'>
    <input type='submit' name='act'
      value='Check' /></form>";
 ?>
 </body></html>

Storing a Session Variable
The correct way to store and retrieve session variables is to use the PHP $_SESSION variable.
2.php
<?php  session_start( );  ?>

<html><body>
<?php
 echo "Page 2";
 echo "Name = " . $_SESSION[customer];
 echo "<form method='post' action='3.php'>
   <input type='submit' name='act'
     value='Check' /></form>";
?>
</body></html>

Destroying a Session
If you wish to delete some session data, you can use the unset or the session_destroy function. The unset function is used to free the specified session variable.
3.php
<?php  session_start( );  ?>

<html><body>
<?php
 echo "Page 3";
 echo "Name = " . $_SESSION[customer];
 session_destroy( );
 echo "<form method='post' action='4.php'>
   <input type='submit' name='act'
     value='Check' /></form>";
?>
</body></html>

You can also completely destroy the session by calling the session_destroy function, which will reset your session and you will lose all your stored session data.
4.php
<?php  session_start( );  ?>

<html><body>
<?php
 echo "Page 4";
 echo "Name = " . $_SESSION[customer];
 echo "<form method='post' action='5.php'>
   <input type='submit' name='act'
     value='Home' /></form>";
?>
</body></html>

If the script 5.php is activated, the page will be re-driected to the home page start.html.
5.php
<?php
  if ( $_POST["act"] == "Home" ) {
    header( "Content-type: text/html" );
    header( "Location: start.html" );
  }
?>




      If God had really intended man to fly,    
      He’d make it easier to get to the airport.    
      — Jonathan Winters