【问题标题】:the page isn't redirecting properly in php页面未在 php 中正确重定向
【发布时间】:2015-08-01 07:01:52
【问题描述】:

当我使用非管理员用户登录时,它会提示我该错误

页面没有正确重定向

这是 login.php 页面代码

<?php

require_once ('session.php');
if($_SESSION['userdetail'] !=FALSE){
    header('location: home.php');
            exit();
}
?>
<html>
<head><title>Login</title></head>
<body>
<form action="checklogin.php" method="post" enctype="multipart/form-data">
<table>
<tr><td>UserName:</td><td><input type="text" name="user_name"></td></tr>
<tr><td>Password:</td><td><input type="password" name="password" ></td></tr>

<tr><td colspan="2"><input type="submit" value="Submit"></td></tr>
</table>
</form> 

这是 ckecklogin.php 页面代码

<?php

if(!isset($_POST['user_name']) || !isset($_POST['password'])){
     header('location: login.php?log=error');
            exit();
}

if(empty($_POST['user_name']) || empty($_POST['password'])){
     header('location: login.php?log=error');
            exit();
}


require_once('includes/connection.php');
require_once('includes/book_functions.php');
require_once('includes/user_functions.php');
require_once ('session.php');

$user_name=$_POST['user_name'];
$user=  get_user_by_name($user_name);
db_close();
if(!$user){
    header('location: login.php?log=errusr');
            exit();
}
$password=$_POST['password'];
$user->passhash;
if (password_verify($password, $user->passhash)) {

    $_SESSION['userdetail']=$user;
    $_SESSION['userdetail']->passhash = NULL;
    header('location: home.php');
            exit();
}
else{
     header('location: login.php?log=errusr');
            exit();
}

它将我标头到 home.php 和 firefox 给我这个错误,这是如果我像普通用户一样登录但如果我使用我的管理员帐户登录,我可以正常工作! 有什么问题?

【问题讨论】:

  • $_SESSION['userdetail']-&gt;passhash 你为什么使用-&gt;$_SERVER 是数组而不是对象
  • 这是 $_SESSION 而不是 $_SERVER ,我将我的用户对象放在 $_SESSION['userdetail'] 中,以便将其转换为对象

标签: php firefox redirect header


【解决方案1】:

在发送标头之前您不应该有任何输出

不好:

    print_r($_SESSION['user_info']);
   header('location: home.php');

好:

   header('location: home.php');

不好:

echo $password=$_POST['password'];
echo $user->passhash;

好:

"emptyness"

【讨论】:

  • 我把它放在确保登录是正确的,但这是问题的原因
  • 不客气。请记住,您可以将其用于某种调试,但之后替换。发送标头之前应该没有输出
  • 感谢 Fox Malder 先生,我会考虑到这一点 :)
【解决方案2】:

使用ob_clean 删除标头错误也可以使用window.location 或通过javascript 重定向

【讨论】:

    猜你喜欢
    • 2015-07-17
    • 2017-11-28
    • 1970-01-01
    • 2017-01-18
    • 2011-08-11
    • 2018-06-08
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多