<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);

//  ini_set('session.use_trans_sid', true);		//in case it is not set in the php.ini file
    session_start();                                    //Obtain handle to session variables
     
    if (! $_SESSION['name'])                            //if there is no name saved in session                          
        header("Location: session_start.php");          //redirect to start session page
?>

<html>
<head>
<meta http-equiv=refresh content=60>                    <!-- force a refresh (just for fun) -->
<body bgcolor=lightyellow>
<h1 style="display:inline">Access Session Variables </h1> 

<?php
    
    $name     = $_SESSION['name'];                      //get user name
    $duration = time() - $_SESSION['start_time'];       //compute session duration
    $visit    = ++$_SESSION['counter'];                 //add 1 to number of time visited 

    if (! $_SESSION['start_time']) $duration = 0;

    print "<b> (Welcome $name) </b><br> \n";    
    print "<br>You've been active for: <b>$duration</b> seconds \n"; 
    print "<br>You have visited:       <b>$visit   </b> times   \n"; 
?>

<br><br>
<form method=get name=frm>
<table>
<tr><td>Enter item to your cart<td> <input type=text name=item>
<tr><td>Enter quantity<td><input type=text name=value>
</table>
<br>
<input type=submit value="Add to Cart">
</form>

<?php
    $newItem  = $_GET['item'];                          //get all form fields
    $newValue = $_GET['value'];

    if ($newItem && $newValue)                          //if both item and value exist
        $_SESSION[$newItem] = $newValue;                //add item & value to session
?>

<table border=2 bgcolor=tan>
<tr bgcolor=cyan><th><i>Session Variable Name<th><i>Session Variable Value</th>

<?php

    foreach ($_SESSION as $name => $value)              //loop through $_SESSION array
    {
        if ($name == 'start_time')
            $value = date('r',$value);
        print "<tr><td><b>$name <td>$value";            //print variable name & value
    }
?>

</table>
<hr>
<center>
<a href=session_use.php> refresh session</a> |  
<a href=session_type.php>session info</a>    |
<a href=session_xit.php> exit session</a>
</center>
<?php include "../include.php"; ?>              <!-- hyperlink to see the code -->
</body>
</html>