Learning PHP Sessions
| From: | Matthew Tedder | Date: | Tue, 30 Oct 2001 20:30:05 +0000 |
| Subject: | Learning PHP Sessions | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-13874@lists.php.net to get a copy of this message | ||
<?php
/*
Hi,
I'm new to PHP and am having trouble understanding how to use PHP
sessions. My book tells in near the beginning how to start them and register
session variables, but I can't figure out how to destroy a session or later
read those session variables. I'm also trying to do this across frames, but
can't even get it to work within a single page.
Here's what I've learned so far and what my problems are:
*/
/* To start a session */
session_start();
/* To register a session variable */
session_register("myvar");
$myvar = "some value";
/*
PROBLEM #1: From the above commands, I get a $PHPSESSID that seems to be
globally available for use, but I cannot seem to read my values back out of
the registered session variable from anywhere... I tried:
*/
print "$myvar\n"; /* and absolutely nothing is printed */
/* To destroy a session */
session_destroy();
/*
PROBLEM #2: This says there is no session to destroy. It's rather strange
because I can still print the $PHPSESSID value..
I've attached my code...
*/
?>
<?php /*=====<< Check for New User Session >>=====*/ if(!isset($PHPSESSID)) { session_start(); session_register("UserName"); session_register("UserPage"); $UserName = "guest"; $UserPage = "Home"; }; /*=====<< Main Switchboard for Pages >>=====*/ include "includes/bodyhead.html"; switch($action) { case "Home": ShowHome(); break; case "Login": ShowLogin(); break; case "AuthUser": AuthUser($UserName, $UserPassword); break; case "Logout": ShowLogout(); break; default: ShowMessage("Service Not Yet Implemented"); break; } include "includes/bodyfoot.html"; /*=====<< Function To Show Home Page >>=====*/ function ShowHome() { include "includes/home.html"; } /*=====<< Function To Show Login Page >>=====*/ function ShowLogin() { print "<form type=put action=right.php>\n"; print "<input type=hidden name=action value=AuthUser>\n"; print "<table border=1 width=100%>\n"; print "<tr><td bgcolor=yellow align=center>USER AUTHORIZATION</td></tr>\n"; print "</table>\n"; print "<table align=center>\n"; print "<tr>\n"; print "<td><font color=yellow><b>User Name:</b></font></td>\n"; print "<td align=center><input type=text name=UserName length=16 maxlength=16></td>\n"; print "</tr>\n"; print "<tr>\n"; print "<td><font color=yellow><b>Password:</b></font></td>\n"; print "<td align=center><input type=password name=UserPassword length=16 maxlength=16></td>\n"; print "</tr>\n"; print "<tr>\n"; print "<td align=center> </td>\n"; print "<td align=center><input type=submit name=UserLogin value='USER LOGIN'></td>\n"; print "</tr>\n"; print "</table>\n"; print "<table border=1 width=100%>\n"; print "<tr><td bgcolor=yellow align=center>UNAUTHORIZED ENTRY IS PUNISHABLE BY LAW</td></tr>\n"; print "</table>\n"; print "</form>\n"; } /*=====<< Function Authenticate User (from Login Page) >>=====*/ function AuthUser($User, $User) { if($User == "guest") { ShowMessage("For guest users, authorization is not required. However, priviledges are limited."); } else { ShowMessage("Authorization Feature Not Yet Implemented"); } } /*=====<< Function To Logout User >>=====*/ function ShowLogout() { ShowMessage("Your Session Has Been Terminated.\nThank You for Properly Logging Out, $UserName!"); session_destroy(); } /*=====<< Function To Show Generic Messages >>=====*/ function ShowMessage($message) { print "<br>\n"; print "<table border=2 align=center>\n"; print "<tr><td align=center><font color=yellow>$message</font></td></tr>\n"; print "</table>\n"; } ?>
<?php /*=====<< Check for New User Session >>=====*/ if(!isset($PHPSESSID)) { session_start(); session_register("UserName"); session_register("UserPage"); $UserName = "guest"; $UserPage = "Home"; }; /*=====<< Main Switchboard for Pages >>=====*/ include "includes/bodyhead.html"; switch($action) { case "Home": ShowHome(); break; case "Login": ShowLogin(); break; case "AuthUser": AuthUser($UserName, $UserPassword); break; case "Logout": ShowLogout(); break; default: ShowMessage("Service Not Yet Implemented"); break; } include "includes/bodyfoot.html"; /*=====<< Function To Show Home Page >>=====*/ function ShowHome() { include "includes/home.html"; } /*=====<< Function To Show Login Page >>=====*/ function ShowLogin() { print "<form type=put action=right.php>\n"; print "<input type=hidden name=action value=AuthUser>\n"; print "<table border=1 width=100%>\n"; print "<tr><td bgcolor=yellow align=center>USER AUTHORIZATION</td></tr>\n"; print "</table>\n"; print "<table align=center>\n"; print "<tr>\n"; print "<td><font color=yellow><b>User Name:</b></font></td>\n"; print "<td align=center><input type=text name=UserName length=16 maxlength=16></td>\n"; print "</tr>\n"; print "<tr>\n"; print "<td><font color=yellow><b>Password:</b></font></td>\n"; print "<td align=center><input type=password name=UserPassword length=16 maxlength=16></td>\n"; print "</tr>\n"; print "<tr>\n"; print "<td align=center> </td>\n"; print "<td align=center><input type=submit name=UserLogin value='USER LOGIN'></td>\n"; print "</tr>\n"; print "</table>\n"; print "<table border=1 width=100%>\n"; print "<tr><td bgcolor=yellow align=center>UNAUTHORIZED ENTRY IS PUNISHABLE BY LAW</td></tr>\n"; print "</table>\n"; print "</form>\n"; } /*=====<< Function Authenticate User (from Login Page) >>=====*/ function AuthUser($User, $User) { if($User == "guest") { ShowMessage("For guest users, authorization is not required. However, priviledges are limited."); } else { ShowMessage("Authorization Feature Not Yet Implemented"); } } /*=====<< Function To Logout User >>=====*/ function ShowLogout() { ShowMessage("Your Session Has Been Terminated.\nThank You for Properly Logging Out, $UserName!"); session_destroy(); } /*=====<< Function To Show Generic Messages >>=====*/ function ShowMessage($message) { print "<br>\n"; print "<table border=2 align=center>\n"; print "<tr><td align=center><font color=yellow>$message</font></td></tr>\n"; print "</table>\n"; } ?>