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

时间:2016-05-18 06:41:35

标签: php

的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>

Echo&#34;为什么它不能导致view_record.php&#34;尝试登录时显示。但是如果使用标题,它不会引导我查看页面,而是将我重定向到登录表单页面。在它工作之前,然后在功能登录中更改了代码,它不再起作用。不知道为什么标题不再起作用。

帮助表示赞赏!

更新 - 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>

1 个答案:

答案 0 :(得分:0)

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

<?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;来查看错误。

祝你好运!