网页登录不重定向 - php

时间:2017-10-15 20:17:49

标签: php mysqli

我的登录页面的本地版本正常工作,但在线版本不会重定向到管理页面。可能的原因是什么?以下是用于登录管理页面的代码。我没有收到任何错误(除非我输错了用户/密码),但它没有重定向到管理页面。

登录操作

<?php
$username = "";

if (isset($_POST['login'])) {
  // Process the form

  // validations
  $required_fields = array("username", "password");
  validate_presences($required_fields);

  if (empty($errors)) {
    // Attempt Login

        $username = $_POST["username"];
        $password = $_POST["password"];

        $found_admin = attempt_login($username, $password);

    if ($found_admin) {
      // Success
            // Mark user as logged in
            $_SESSION["admin_id"] = $found_admin["id"];
            $_SESSION["username"] = $found_admin["username"];
     redirect_to ('admin.php');
    } else {
      // Failure
      $_SESSION["message"] = "Username/password not found.";
    }
  }
} else {
  // This is probably a GET request

} // end: if (isset($_POST['submit']))

?>

尝试登录功能

function attempt_login($username, $password) {
        $admin = find_admin_by_username($username);
        if ($admin) {
            // found admin, now check password
            if (password_check($password, $admin["password"])) {
                // password matches
                return $admin;
            } else {
                // password does not match
                return false;
            }
        } else {
            // admin not found
            return false;
        }
    }

功能redirect_to

function redirect_to($new_location) {
        header("Location: " . $new_location);
        exit;
    }

我收到了这个错误:

  

[15-Oct-2017 17:03:32 America / New_York] PHP警告:无法修改   标题信息 - 已经发送的标题(输出开始于   /home/qvthqtxu/public_html/christasite/includes/session.php:28)in   /home/qvthqtxu/public_html/christasite/includes/functions.php上线   4

1 个答案:

答案 0 :(得分:2)

这是开发人员的常见问题。最好使用HTML重定向。

<meta http-equiv="refresh" content="0; url=http://example.com/" />
<meta http-equiv="refresh" content="0; url=./admin.php" />
<meta http-equiv="refresh" content="0; url=./admin/" />

不要忘记嵌入如下的PHP代码。

echo "<meta http-equiv=\"refresh\" content=\"0; url=./admin.php\" />";