I've been trying to make a login/logout in same page.
I just use 1 simple database table, and the session variable.
Its not working properly, here is my code
<?PHP
session_start();
echo <<<delimiter1
<html>
<title> Logging </title>
<body>
delimiter1;
if (!isset($_GET['LoggedIn'])){
if(!isset($_GET['FormSubmitted'])){
echo <<<delimiter2
<form id="form1" name="form1" method="post" action="index.php?FormSubmitted=1">
<table>
<tr>
<td>User : </td>
<td><input name="username" type="text" id="username" /></td>
</tr>
<tr>
<td>Password : </td>
<td><input name="password" type="password" id="password" /></td>
</tr>
</table>
<input name="Login" type="submit" id="Login" value="Login" />
</form>
delimiter2;
}
else{
if($_POST['Login'])// If clicked on Login button
{
$username=$_POST['username'];
$password=$_POST['password']; // Encrypt password with md5() function.
mysql_connect("localhost", "root", "");
mysql_select_db("testdatabase");
// Check matching of username and password.
$result=mysql_query("select * from users where username='$username' and password='$password'");
if(mysql_num_rows($result)!='0'){// If match.
session_register("username");/*Register*/
$_SESSION["username"] = $username;/*Assign*/
if(!session_is_registered("username")){// if session variable "username" does not exist.
header("location:index.php"); // Re-direct to index.php
}
else{
echo "Hello and Welcome" .$_SESSION['username'] ;
echo <<<delimiter3
<a href="index.php?LoggedIn=0">Logout</a>
delimiter3;
}
}
else{ //If not match.
$message="--- Incorrect Username or Password ---";
}
}//End Login authorize check.
}
}
else{
unset($_GET['LoggedIn']);
session_destroy();
header("location:index.php");
}
echo <<<delimiter4
</body>
</html>
delimiter4;
?>