Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As an example, in facebook if we log in once and without logging out click on the back button in the browser it reloads the home page again. But in the login page which I made using PHP is I press the back button in the browser it takes me back to the login page in spite of the fact that I am already logged in.

What I have tried:

The below is the code for login page:

include 'left.php';
mysql_connect('localhost','root','');
mysql_select_db('onlineexam');

if(isset($_POST['submit']))
{
   session_start();
   $_SESSION['email'] = $_POST['email'];
   $_SESSION['pass'] = $_POST['password'];
   $email = $_SESSION['email'];
   
   $query = mysql_query("select name,password,email from students where email = '$email'") or die("you are not registered");
   $arr = mysql_fetch_assoc($query);

   if ($arr['password']==$_SESSION['pass'])
   {
      $name = $arr['name'];
      echo "<div class="righttop">
            <h1>hello $name</h1><br>
            <form method='post' action='index.php'>
            <input type='submit' name='logout' value='LOGOUT'>
            </form>
            </br></div>";  
   }
   else
   {
      echo "<div class="righttop">
            <h1>Wrong Password. Try Again!!</h1>
            <?php include 'righttop.php'; ???>
            </div>";
      session_destroy();
      session_cache_expire();
   }
}
elseif(isset($_SESSION['email']))
{
   echo "halaluuya";
}
else
{
   header("Location: http://localhost/project/index.php");
   exit();
}

include 'rightbottom.php';
?>
Posted
Updated 11-Jul-16 9:01am
v2

1 solution

Try this code :

<?php
include 'left.php';
$conn = mysqli_connect('localhost','root','');
mysqli_select_db($conn, 'onlineexam');

if(isset($_POST['submit']) && !(isset($_SESSION['email'])))
{
    session_start();
    $_SESSION['email'] = $_POST['email'];
    $_SESSION['pass'] = $_POST['password'];
    $email = $_SESSION['email'];

    $query = mysqli_query($conn, "select name,password,email from students where email = '$email'") or die("you are not registered");
    $arr = mysqli_fetch_assoc($query);

    if ($arr['password']==$_SESSION['pass'])
    {
        $name = $arr['name'];
        echo "<div class="righttop">
            <h1>hello $name</h1>
 
            <form method='post' action='index.php'>
            <input type='submit' name='logout' value='LOGOUT'>
            </form>
            </br></div>";  
   }
    else
    {
        echo "<div class="righttop">
            <h1>Wrong Password. Try Again!!</h1>
            <?php include 'righttop.php'; ???>
            </div>";
      session_destroy();
      session_cache_expire();
   }
}
elseif(isset($_SESSION['email']))
{
    echo "halaluuya";
}
else
{
    header("Location: http://localhost/project/index.php");
    exit();
}

include 'rightbottom.php';
?>


I have added another condition in the IF block as
if(isset($_POST['submit']) &amp;&amp; !(isset($_SESSION['email'])))


Hope this will help you though I haven't tested it. Please let me know if you still find any issue or in case if it doesn't work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900