登录身份验证不适用于我的网站

时间:2016-06-27 14:05:56

标签: php html login

网站代码如下。登录无法通过我所做的事情进行身份验证。首先,如果没有登录,它将按预期重定向到登录页面。然后,在我清楚地正确提供登录详细信息后,它不会将我重定向到我想要的网站。相反,它将保留在登录页面上。请帮帮我......



<!--This is the page that I want to redirect after successful login-->


<?php
session_start();

if($_SESSION['loggedIn'])
{
  header('Location: restaurant.php');
}
else
{
  header('Location: login.php');  
}
?>

<html lang="en">
<head>
  <title>Welcome to Foodline</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="">
  <meta name="author" content="">
  <link rel="stylesheet" href="bootstrap/dist/css/bootstrap.min.css">
  <link href="css/simple-sidebar.css" rel="stylesheet">

  <script src="bootstrap/js/jquery.min.js"></script>
  <script src="bootstrap/js/bootstrap.min.js"></script>
  <script type="text/javascript" src="js.js"></script>
  <script type="text/javascript" src="jquery.js"></script>

  <style>
    /* Remove the jumbotron's default bottom margin */ 
     .jumbotron {
      margin-bottom: 0;
    }
   
    /* Add a gray background color and some padding to the footer */
    footer {
	  background-color: #f2f2f2;
      padding: 25px;
    }
  </style>
</head>
<body>

<div class="jumbotron">
  <div class="container text-center">
    <h1><font face="Analecta">FOODLINE</font></h1>
    <p>We provide the best service for our costumers</p>
  </div>
</div>

 <nav class="navbar navbar-inverse" data-spy="affix" data-offset-top="197">
  <div class="container-fluid">
    <div class="navbar-header">
     <a class="navbar-brand"><font face="Analecta" color="white">>Restaurants<</font></a>
    </div>

    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav">
                <li>
                    <a href="foodline.php">Hamro Didi (HD)</a>
                </li>
                <li>
                    <a href="#">HK</a>
                </li>
                <li>
                    <a href="#">Junu Hotel</a>
                </li>
                <li>
                    <a href="#">Junction Cafe</a>
                </li>
                <li>
                    <a href="#">Laxmi Hotel</a>
                </li>
            </ul>
        </div>
    </div>
    </nav>


<footer class="container-fluid text-center">
<p>Foodline Official Website &copy</p>  
<p align="center">Logged in as: <div id="username" align="center"> <span class="glyphicon glyphicon-log-in"></span><?php
if(isset($_GET['id'])){
echo ' '.$_GET['id'];
}
else {
echo '(write) a 404 page';
}
?>
</div>
</p>
</footer>

</div>
&#13;
&#13;
&#13;

&#13;
&#13;
<!--This is login.php-->


<?php
//session_start();
include("connection.php");
$msg='';

if($_SERVER["REQUEST_METHOD"] == "POST")
{
    // username and password sent from form 
    $username = $_POST['username'];
    $password = $_POST['password']; 

    // To protect MySQL injection
    $username = stripslashes($username);
    $password = stripslashes($password);
    $username = mysql_real_escape_string($username);
    $password = mysql_real_escape_string($password);

  //Input Validations
  if($username == '') {

$_SESSION["login_user"] = $username;   $msg = "Username missing";
    header("location: login.php?msg=$msg");
  }
  if($password == '') {
    $msg = "Password missing";
    header("location: login.php?msg=$msg");
  }
  //Create query
  $qry="SELECT * FROM user WHERE user_name='$username' AND user_password='$password'";
  $result =mysql_query($qry)or die(mysql_error());
  $output=mysql_fetch_assoc($result);
  //Check whether the query was successful or not
      if(!empty($output)) {
      //Login Successful
      $_SESSION['name']= $username;
      $_SESSION['loggedIn'] = true;  
      header("location:restaurant.php?id=$username");
     
    }
    else {
      //Login failed
      $msg= "user name and password not found";
      header("location:login.php?msg=$msg");
     
      }
}
?>


<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="bootstrap/dist/css/bootstrap.min.css">
  <script src="bootstrap/js/jquery.min.js"></script>
  <script src="bootstrap/js/bootstrap.min.js"></script>
  
  <style>
  .jumbotron {
      margin-bottom: 0;
    }
  </style>
</head>
<body>

<div class="jumbotron">
  <div class="container text-center">
    <h1><font face="Analecta">FOODLINE</font></h1>      
    <p>We provide the best service for our costumers</p>
  </div>
</div>

<nav class="navbar navbar-inverse" data-spy="affix" data-offset-top="197">
  <div class="container-fluid">
    <div class="navbar-header">
     <a class="navbar-brand" href="index.php">Logo</a>
    </div>
    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav">
        <li class="active"><a href="index.php">Home</a></li>
        <li><a href="restaurant.php">Restaurants</a></li>
        
        <li><a href="contact-form/index.html">Contact</a></li>
      </ul>
     <ul class="nav navbar-nav navbar-right">
      <li><a href="register.php"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
      <li><a href="login.php"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
    </ul>
    </div>
  </div>
</nav>

<div class="container">
  <h2><font face="Analecta">>Login from here<</font></h2>
  <form role="form" name="login" action="login.php" method="post" accept-charset="utf-8">
    <div class="form-group">
      <label for="username">Username:</label>
      <input type="text" class="form-control" name="username" placeholder="Enter username" required>
    </div>
    <div class="form-group">
      <label for="password">Password:</label>
      <input type="password" class="form-control" name="password" placeholder="Enter password" required>
    </div>
    <div class="checkbox">
      <label><input type="checkbox"> Remember me</label>
    </div>
    <button type="submit" class="btn btn-default" value="login">Submit</button>
    <br>
    <br>
    
    <?php
      $msg = (isset($_GET['msg']) ? $_GET['msg'] : null);  //GET the message
      if($msg!='') echo '<p>'.$msg.'</p>'; //If message is set echo it
    ?>
    
  </form>
  <p>Not a user yet? Sign up <a href="register.php">here</a></p>
</div>

<footer class="container-fluid text-center">
  <p>Foodline Official Website &copy</p>  
  <p>Get deals:
    <a href="register.php"><span class="glyphicon glyphicon-menu-right"></span>SignUp</a>
  </p>
</footer>

</body> 
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

取消注释:

//session_start();

从login.php的第5行开始,改为:

if(! $_SESSION['loggedIn']) {
  header('Location: login.php');  
}

在restaurant.php。

相关问题