Currently making a small application on html, which utilises php for user login which is connected to a database. User login works as required, users can login, register, logout and relogin as a normal login behaviour should function.
However, when a user logs out, they are able to immediately 're-enter' the application by using the browser BACK button, which is an obvious security flaw.
I am trying to use sessions in creating a solution to the above issue.. any help would be highly appreciated. My application is currently broken down into the following pages.
1) login.php (simple username & password entered into a form, form executes logincheck.php
2) logincheck.php (SEE BELOW)
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="site.css">
</head>
<body>
<div class="frame">
<div class="headerf"></div>
<div class="contentf">
<h3>Incorrect user credentials.</h3>
<?php
include("info.inc.php");
$comm=@mysql_connect(localhost,$username,$password);
$rs=@mysql_select_db($database) or die( "Unable to select database");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM users WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "index.php"
$_SESSION["username"] = $myusername;
$_SESSION["password"] = $mypassword;
header("location:../index.php");
}
else {
echo "Please return to the log-in page.";
}
?>
<form name="form" method="post" action="../login.php"><br>
<td><input type="submit" name="Submit" value="Back"></td>
</div>
</div>
</body>
</html>
3) index.php (if successful login, user is redirected to this page, where they have access to the application and can navigate throughout as normal).
4) logout.php (session is ended, user is redirected to login page)
I currently have session start in the login, and session end in the logout pages. How can i implement a secure form of loggin in ? Have been trying online examples, but to no avail, i.e. http://blog.teamtreehouse.com/how-to-create-bulletproof-sessions
My ideal fix would be, instead of a user being able to 'BACK' into the application after logout, they are redirected to the login page again or username and password is prompted for again
any help is highly appreciated!