Accessing Session Variables

You want to access the sessions variables.

Use the session_start() and $_SESSION array.

<?php
// Enable output buffering. No output is sent from the script
// (other than headers). It is saved in an internal buffer
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>Accessing Session Variables</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php

// Start the session
session_start();
echo "<p>Welcome to our site $_SESSION[username]!</p>";
// use date(), g=hour, i=min, a=AM or PM
echo "<p>You have been logged in since ",
date('g:i a',$_SESSION[loggedin]), ".</p>";
// display the session ID
echo "<p>The session ID is: ", session_id(), ".</p>";

echo "<p><a href=\"example_d.php\">Log out</a></p>";
echo "<p><a href=\"example_c.php\">Create the session variables?</a></p>";
echo "<p><a href=\"$_SERVER[SCRIPT_NAME]\">Try again?</a></p>";

?>
</p>
</body>
</html>
<?php
// Flush the buffer and end output buffering.
ob_end_flush();
?>

View the effect


Sessions in PHP | Introduction | Basic Session Functionality | Creating a Session | Accessing Session Variables | Deleting a Session | Implementing a Login System with Sessions | Saving Arrays in a Session | Using Cookies with Session | Sessions without Cookies | Potential Session Problems
© 2008: Hann So
email: hso@voyager.deanza.edu