#native_company# #native_desc#
#native_cta#

Using Session – Basic Example

By John Diller
on March 5, 2002

Version: 1.0

Type: Sample Code (HOWTO)

Category: HTTP

License: GNU General Public License

Description: Start a session, register a variable i nthe session, use the session variable, unregister the session variable, destroy the session.

/*  This is three pages in total.


<?

// start a session and register your first variable

	session_start();
	session_register("sess_var");

	$sess_var = "Hello World!";

?>

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="UltraEdit">
<meta NAME="keywords" CONTENT="session, test, variable">
<meta NAME="description" CONTENT="Session Testing and walk through">
<title>Session Test And Walk Through</title>


</head>


<? include("header.inc") ?>


<CENTER>


<?
	// This will print sess_var to screen so we know it exists
        
	echo "$sess_var = $sess_var<BR>";

?>

<P>

<A HREF = "session_test2.php">Confirm $sess_var was registered</A>

</CENTER>

</BODY>
</HTML>



Page 2


<?

	session_start();


?>

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="UltraEdit">
<meta NAME="keywords" CONTENT="session, test, variable">
<meta NAME="description" CONTENT="Session Testing and walk through">
<title>Session Test And Walk Through Page 2</title>


</head>





<CENTER>

<P>

<?
	// Print the sess_var again to see that we did indeed pass it in the session

	echo "$sess_var = $sess_var<BR>";

	// Now let's unregister sess_var from the session

	session_unregister("sess_var");

?>

<P>

<A HREF = "session_test3.php">Confirm $sess_var was unregistered</A>

</CENTER>

</BODY>
</HTML>


Page 3


<?

	session_start();


?>

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="UltraEdit">
<meta NAME="keywords" CONTENT="session, test, variable">
<meta NAME="description" CONTENT="Session Testing and walk through">
<title>Session Test And Walk Through Page 3</title>


</head>

<CENTER>

<?

	/*
	Print out session_variable again, it shouldn't
	exist because we unregistered it in the previous page
	*/

	echo "$sess_var = $sess_var<BR>";

	// Now we destroy the session altogether

	session_destroy();
	echo "<BR>session has now been destroyed";
?>

<P>
<A HREF = "session_test.php">Back to beginning</A>

</CENTER>

</BODY>
</HTML>