I am developing a simple user details management app. where I have a login screen. I have designed it using mysql. But if i use the session_start() in the login page and use session_destroy in the logout page, which can be accessed thru the page the user gets into after successfully loging in, i get an error saying " Warning: session_destroy() [function.session-destroy]: Trying to destroy uninitialized session ".
How will I initialize a session. How will i maintain the session between login and logout, when I have some 2 or 3 pages inbetween the login and logout pages ?
--------------login page------------------
Code: Select all
<?php session_start();?>
<html>
<head>
<title> User Information Management </title>
</head >
<body>
<?php
if ($_POST['_submit_check']) { // check whether uname and passwd are entered, if else show the form
process_form();
} else {
show_form();
}
?>
</body>
</html>
<?php function show_form() { // Display the login form ?>
<div align="center"><br /><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" > <!-- Login Form-->
<table><tr>
<td>User Name: </td>
<td><input type="text" name="user_name" maxlength="20" /></td>
</tr><tr>
<td>Password: </td>
<td><input type="password" name="pass_word" maxlength="20" /></td>
</tr><tr>
<td align="right" colspan="2" ><input type="submit" name="login_button" value="Login" style="background-color:white; " /></td>
<td><input type="hidden" name="_submit_check" value="1"/> </td>
</tr>
</table>
</form >
</div>
<?php } ?>
function process_form() { //Process the form - check for username and password
$link = mysql_connect('localhost', 'root', 'root');
if ( ! $link ) { die("Can't Connect: " .mysql_error()); }
$uname = $_POST['user_name'];
$pword = $_POST['pass_word'];
mysql_select_db('hdp_um');
$query = "SELECT * FROM login_table WHERE u_name = '$uname' AND p_word = '$pword'";
$q = mysql_query($query);
if ( ! mysql_num_rows($q)) { //if there is any row returned from query, proceed
print "Login Failed - Wrong Username or Password";
exit(); }
$_SESSION['user_name'] = $uname;
selection_screen(); } ?>Code: Select all
<?php
session_destroy();
?>
<html>
<head>
<title> Logged Out !</title>
<link rel="stylesheet" type="text/css" href="trial_style.css" />
</head>
<body>
<? //echo $_SESSION['user_name']; ?>
<div><h3> User Logged Off From Current Session!</h3></div>
<br />
<div><p>Do You Wish to <a href="dblogin.php"> Login Back ? </a></p></div>
</body>
</html>Selection screen is the link to next page, where i have placed the 'logout' page link, either as <a href... or as a submit button.
Can anybody point me to good tutor/web site on sessions in PHP ?