登录后,它会将我重定向回登录表单


Upon login, it redirect me back to login form

Login.php

    <?php 
        include 'config/database.php';
        include 'functions/functions.php';
        $Db = "users";
        $user = $_POST['emailaddress'];
        $pass = $_POST['password'];
        login($user, $pass, $con, $Db);
        ?>

Functions.php

    if (!function_exists('login')) { 
  function login($user, $pass, $link, $db){
      if(!$link->select_db($db)){
        echo "Failed Database";
      }
     if(!empty($_POST)){
      $person = sanitize($user, "html", $link);
      $secret = sanitize($pass, "html", $link);
      $query = "SELECT * FROM users WHERE email = ? AND pass = ?";
      if (!$stmt = $link->prepare($query)) {
        echo "Prepare failed: (" . $link->errno . ") " . $link->error;
      }
      $stmt->bind_param('ss', $person, $secret);
      if (!$stmt->execute()) {
        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
      }
      $result = $stmt->get_result();
      $stmt->store_result();
      $check = $result->num_rows;
      if($check){
        $items = $result->fetch_assoc();
        session_start();
        $_SESSION['loggedin'] = true;
        echo "why cant it lead to view_record.php";
        //header("Location: view_record.php");
      }
      else{
        header("Location: index.php?error=true");
      }
        exit;
        $query->free_result();
        $link->close();
    }
  }
}

HTML登录

<form id="login" name="login-form" method="POST" action="login.php">
   <div class="form-group">
      <label for="emailaddress">Email address</label>
      <input type="email" class="form-control" name="emailaddress" placeholder="Email Address">
    </div>
    <div class="form-group">
      <label for="password">Password</label>
      <input type="password" class="form-control" name="password" placeholder="Password">
    </div>
    <button type="submit" class="btn btn-login">Login</button>
</form>

当尝试登录时,回声显示"为什么它不能导致view_record.php"。但若使用头,它不会引导我查看页面,而是将我重定向到登录表单页面。在它工作之前,然后更改了函数登录中的代码,它就不再工作了。不确定为什么标头不再工作。

感谢帮助!

更新-view_record.php

  <body>
  <?php include 'layout/header.php' ?>
  <div class="container center-block">
    <h1>View List</h1>

       <?php
            include 'config/database.php';
            include 'functions/functions.php';
            if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
                viewList("test", "id", $con);
              }
            else{
              header("Location: index.php"); 
            }       
       ?>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
</body>

我对您的代码做了一些小的更改:

<?php
session_start();
include 'config/database.php';
include 'functions/functions.php';
$Db = "users";
$user = $_POST['emailaddress'];
$pass = $_POST['password'];
login($user, $pass, $con, $Db);
?>
Functions.php
if (!function_exists('login')) {
    function login($user, $pass, $link, $db) {
        if (!$link->select_db($db)) {
            echo "Failed Database";
        }
        if (!empty($_POST)) {
            $person = sanitize($user, "html", $link);
            $secret = sanitize($pass, "html", $link);
            $query = "SELECT * FROM users WHERE email = ? AND pass = ?";
            if (!$stmt = $link->prepare($query)) {
                echo "Prepare failed: (" . $link->errno . ") " . $link->error;
            }
            $stmt->bind_param('ss', $person, $secret);
            if (!$stmt->execute()) {
                echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
            }
            $result = $stmt->get_result();
            $stmt->store_result();
            $check = $result->num_rows;
            if ($check) {
                $items = $result->fetch_assoc();
                $_SESSION['loggedin'] = true;
                echo "why cant it lead to view_record.php";
                //header("Location: view_record.php");
                // # -- Line added
                exit;
            } else {
                header("Location: index.php?error=true");
                // # -- Line added
                exit;
            }
            // This exit is not necassary
            exit;
            // This will never be executed
            $query->free_result();
            $link->close();
        }
    }
}

然而,这并不能解决您的问题。它保持重定向的唯一原因可能是数据库连接不正确或没有返回正确的结果。您可以通过在整个代码中使用var_dump()exit;来调试它,看看哪里出了问题。

祝你好运!