1

I have designed the login page as shown below with the functionality to check from the database. I want the site to redirect the user to their specified page displaying their reports page (view_report.php) after successful login. For some reason, after entering the details, the page returns back to the same page instead of not logging in and doesn't throw any errors as well. I'm not sure as to where I went wrong. Thanks for the help in advance. :-)

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
  ----header area----
</head>
<body>
 <div class="logo"></div>
  <div class="login"> <!-- Login -->
    <h1>Login</h1>

    <form class="form" method="POST" action="index.php">

      <p class="field">
        <input type="text" name="login" placeholder="Restaurant id" required/>
        <i class="fa fa-user"></i>
      </p>

      <p class="field">
        <input type="password" name="password" placeholder="Password" required/>
        <i class="fa fa-lock"></i>
      </p>

      <p class="submit"><input type="submit" name="commit" value="Login"></p>

      <p class="remember">
        <input type="checkbox" id="remember" name="remember" />
        <label for="remember"><span></span>Remember Me</label>
      </p>

      <p class="forgot">
        <a href="#">Forgot Password?</a>
      </p>

    </form>
  </div> <!--/ Login-->

<?php
if( $_SESSION["logging"]&& $_SESSION["logged"])
{
     header("Location:view_report.php");    
}
else {
    if(!$_SESSION["logging"])
    {  
    $_SESSION["logging"]=true;
    }
       else if($_SESSION["logging"])
       {
         $number_of_rows=checkpass();
         if($number_of_rows>=1)
            {   
             $_SESSION[user]=$_GET[userlogin];
             $_SESSION['logged']=true;
             header("Location:view_report.php");    
            }
        }
     }

function checkpass()
{
$servername="localhost";
$username="root";
$conn=  mysql_connect($servername,$username)or die(mysql_error());
mysql_select_db("konjam_disc",$conn);
$sql="select * from users where name='$_GET[userlogin]' and password='$_GET[password]'";
$result=mysql_query($sql,$conn) or die(mysql_error());
return  mysql_num_rows($result);
}

?>
</body>
</html>
dishoom
  • 27
  • 6

2 Answers2

0

You cannot redirect or "modify headers" once you send any HTML output so your redirect code must be before you send any HTML content. So your code could be something like this

<?php
session_start();

if( $_SESSION["logging"]&& $_SESSION["logged"])
{
    header("Location:view_report.php");    
} else {
    if(!$_SESSION["logging"])
    {  
    $_SESSION["logging"]=true;
    }
    else if($_SESSION["logging"])
    {
        $number_of_rows=checkpass();
        if($number_of_rows>=1)
        {   
            $_SESSION[user]=$_GET[userlogin];
            $_SESSION['logged']=true;
            header("Location:view_report.php");    
        }
    }
 }

?>

<html>
...

refer this answer

How to fix "Headers already sent" error in PHP

Community
  • 1
  • 1
Xpleria
  • 5,472
  • 5
  • 52
  • 66
  • Thank you for the answer @Neil Martin. I tried. But it didn't work and I'm having the same bug. Any suggestions? – dishoom Dec 08 '14 at 13:23
0

I am not giving actual code. only giving logic.

Use below logic.

session_start();
    if(session logged in){
    header("Location:view_report.php");

    }
    else{
    show your html form;
    }
Prashant Tapase
  • 2,132
  • 2
  • 25
  • 34