| This example show how to handle session. In this example I tried to
focus on a single form that will self_post a html block that will be only
displayed when the user is logged in with correct password. |
<? //start a session session_start();
//check if user is coming from a form if ($_POST[op] == "sos") {
//check username and password if (($_POST[username] != "manas") || ($_POST[password] != "tuktuk123")) {
//handle bad login $msg = "<P><font color=\"#FF0000 \"><strong>Could Not-- Login -Please Try
Again</strong></font></P>"; $show_form = "yes";
} else {
//handle good login $_SESSION[valid] = "yes"; $show_menu = "yes"; }
} else {
//determine what to show if ($_SESSION[valid] == "yes") { $show_menu = "yes"; } else { $show_form = "yes"; } }
//build form block $form_block ="<h1>Login</h1> <form method=POST action=\"$_SERVER[PHP_SELF]\">
$msg
<P><strong>username:</strong><br> <input type=\"text\" name=\"username\" size=15 maxlength=25></P>
<P><strong>password:</strong><br> <input type=\"password\" name=\"password\" size=15 maxlength=25></P>
<input type=\"hidden\" name=\"op\" value=\"sos\">
<P><input type=\"submit\" name=\"submit\" value=\"login\"></P> </FORM>";
//build menu block $menu_block ="<h3>Testing Area for session and mysql admin pages</h3> <P><strong>Administration</strong> <ul> <li><a href=\"show_page1.php\">Page 1</a> <li><a href=\"show_page2.php\">Page 2</a> <li><a href=\"http://spiritofsoccer.com/Index_Files/right_Admin.html\"
target=\"main\">Admin </a> </ul> <P><strong>View Recorsos</strong> <ul> <li><a href=\"show_contactsbyname.php\">Show Contacts, Ordered by Name</a> </ul>";
//assign the block to show to the $display_block variable if ($show_form == "yes") { $display_block = $form_block; } else if ($show_menu == "yes"){ $display_block = $menu_block; }
?> <HTML> <HEAD> <TITLE>Authentication with session</TITLE> </HEAD> <BODY>
<? echo "$display_block"; ?>
</BODY> </HTML>
|
 |
 |
| if your log in is correct 
|
 |
destroying the session<? //start a session session_start();
//check if user is coming from a form if ($_POST[op] == "sos") { $url=='http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']); //check username and password if ((substr($url,-1)=='/')OR (substr($url,-1)=='\\')) { $url =substr($url,0,-1);}
$url.='auth_session_1.php'; header("Location:$url"); exit();
} else {
//handle good login $_SESSION=array(); setcookie('PHPSOSID','', time()-300, '/','',0); session_destroy(); } echo " you are logged out"; ?> Please also cjech unset
|
| |
| Please also check with unset function |
<?php session_start(); if(isset($_SESSION['views'])) $_SESSION['views'] = $_SESSION['views']+ 1; else $_SESSION['views'] = 1;
echo "views = ". $_SESSION['views']; ?>
|
<?php session_start(); if(isset($_SESSION['cart'])) unset($_SESSION['cart']);
?>
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |