Post Session Example - Page 1
session_id() = 2256e31ea64bacf1382b4cc9b790f51a
Post array is emptyPost array follows
Array ( )Session coffee is empty
Session array follows
Array ( [coffee] => )
GOTO Page 2
session_id() = 2256e31ea64bacf1382b4cc9b790f51a
Post array is emptyArray ( )Session coffee is empty
Array ( [coffee] => )
<?php
session_name("sperlt");
session_start(); // establish, or continue, a session
include('../includes/header4.php');
echo('<h1>Post Session Example - Page 1</h1>');
if(!empty($_POST['clear']))
{
// all this does is clear the SESSION array
// when session_start() runs again, the session id will be the same as before.
echo('Clear Session Array!<br>');
setcookie(session_name(), session_id(), -1); // to expire the session
session_unset();
session_destroy();
session_commit();
}
echo('<p>session_id() = ' . session_id() . '</p>');
$coffee = !empty($_SESSION['coffee']) ? $_SESSION['coffee'] : null;
$coffee = !empty($_POST['coffee']) ? $_POST['coffee'] : $coffee;
$_SESSION['coffee'] = $coffee;
if(!empty($_POST))
{
echo('Post array has something<br>');
}
else
{
echo('Post array is empty<br>');
}
echo("Post array follows");
echo('<pre>');
print_r($_POST);
echo('</pre>');
if(!empty($_SESSION['coffee']))
{
echo('Session coffee has something<br>');
}
else
{
echo('Session coffee is empty<br>');
}
echo("Session array follows");
echo('<pre>');
print_r($_SESSION);
echo('</pre>');
?>
<br><br>
<form action="" method="POST">
<input type="text" name="coffee">
<input type="submit" name="submit" value="Fill Coffee">
<input type="submit" name="clear" value="Clear Session Array">
</form>
<br><br>
<a href="post-session-page2.php">GOTO Page 2</a>
<?php
include('../includes/footer4.php');
?>
Last modified: April 06 2022
Line Count: 66