Contents of index.php
< ?php
session_start();
if (isset($_POST['userid']) && isset($_POST['password']))
{
$userid = $_POST['userid'];
$pswd = $_POST['password'];
if ($userid == $pswd)
{
$_SESSION['validid'] = $userid;
}
}
? >
< html >
< head >
< title > Session example < /title >
< /head >
< body >
< ?php
if (isset($_SESSION['validid']))
{
echo("You are logged in as ".$_SESSION['validid']." < br / > ");
echo(" < a href='logout.php' > log out < /a > ");
}
else
{
if (isset($userid))
{
echo("Could not log you in");
}
else
{
echo("You are not logged in");
}
// form to log in
echo(' < form method="POST" action="index.php" > ');
echo(' < table > ');
echo(' < tr > < td > name < /td > ');
echo(' < td > < input type="text" name="userid" > < /td > < /tr > ');
echo(' < td > password < /td > ');
echo(' < td > < input type="password" name="password" > < /td > < /tr > ');
echo(' < tr > < td colspan="2" align="center" > ');
echo(' < input type="submit" value="Log in" > < /td > < /tr > ');
echo(' < /table > < /form > ');
}
? >
< br / >
< a href="members.php" > Members section < /a >
< /body >
< /html >
Contents of members.php
< ?php
session_start();
echo(" < h1 > Member's section < /h1 > ");
if (isset($_SESSION['validid']))
{
echo('You are logged in as '.$_SESSION['validid'].' < br/ > ');
echo('member-only content');
}
else
{
echo('You are not logged in < br / > ');
echo('Please register to see this page');
}
echo(' < a href="index.php" > Back to main < /a > ');
? >
Contents of logout.php
< ?php
session_start();
$old_user = $_SESSION['validid'];
unset($_SESSION['validid']);
session_destroy();
? >
< html >
< body >
< h1 > Log out < /h1 >
< ?php
if (!empty($old_user))
{
echo('Logged out < br/ > ');
}
else
{
echo("You weren't logged in to begin with");
}
? >
< a href="index.php" > Back to main < /a >
< /body >
< /html >