简体   繁体   English

PHP重定向无法成功登录

[英]PHP redirect not working on successful login

I'm teaching myself PHP so this is probably something stupid I don't know yet, but why is my redirect not working... 我正在自学PHP,这可能是我不知道的愚蠢之举,但是为什么我的重定向无法正常工作...

I'm trying to do this: 我正在尝试这样做:

header("Location: members.php?view=$user"); exit;

which doesn't do anything, but when I place a link in die statement upon successful login 这什么也没做,但是当我成功登录后在die语句中放置链接时

die("You are now logged in. Please <a href='members.php?view=$user'>click here</a> to continue.");

I can click on that link and it works fine. 我可以单击该链接,它可以正常工作。

Full Code 完整代码

<?php //login.php
  include_once 'header.php';
  echo "<div class='main'><h3>Please enter your details to log in</h3>";
  $error = $user = $pass = "";

  if (isset($_POST['user'])) {
    $user = sanitizeString($_POST['user']);
    $pass = sanitizeString($_POST['pass']);

    if ($user == "" || $pass == "") {
      $error = "Not all fields were entered<br />";
    }
    else {
      $query = "SELECT user,pass FROM members WHERE user='$user' AND pass='$pass'";

      if (mysql_num_rows(queryMysql($query)) == 0) {
        $error = "<span class='error'>Username/Password invalid</span><br /><br />";
      }
      else {
        $_SESSION['user'] = $user;
        $_SESSION['pass'] = $pass;
        header("Location: members.php?view=$user"); exit;
      }
    }
  }

echo <<<_END
<form method='post' action='login.php'>$error
<span class='fieldname'>Username</span><input type='text' maxlength='16' name='user' value='$user' /><br />
<span class='fieldname'>Password</span><input type='password' maxlength='16' name='pass' value='$pass' />
_END;
?>

<br />
<span class='fieldname'>&nbsp;</span>
<input type='submit' value='Login' />
</form><br /></div></body></html>



<?php //header.php
session_start();
echo "<!DOCTYPE html>\n<html><head><script src='js/osc.js'></script>";
include 'functions.php';

$userstr = ' (Guest)';

if (isset($_SESSION['user'])) {
    $user     = $_SESSION['user'];
    $loggedin = TRUE;
    $userstr  = " ($user)";
}
else {
    $loggedin = FALSE;
}

echo "<title>$appname$userstr</title><link href='css/main.css' rel='stylesheet' type='text/css' />" .
     "</head><body><div class='$appname'>$appname$userstr</div>";

if ($loggedin) {
    echo "<br /><ul class='menu'>" . 
     "<li><a href='members.php?view=$user'>Home</a></li>" . 
     "<li><a href='members.php'>Members</a></li>" . 
     "<li><a href='friends.php'>Friends</a></li>" . 
     "<li><a href='messages.php'>Messages</a></li>" . 
     "<li><a href='profile'>Edit Profile</a></li>" . 
     "<li><a href='logout.php'>Log Out</a></li></ul><br />";
}
else {
    echo ("<br /><ul class='menu'>" . 
    "<li><a href='index.php'>Home</a></li>" . 
    "<li><a href='signup.php'>Sign Up</a></li>" . 
    "<li><a href='login.php'>Log In</a></li></ul><br />" . 
    "<span class='info'>&#8654; You must be logged in to view this page.</span><br /><br />"
    );
}

?>

I've tried removing the first echo at the top, as well as the $error message. 我尝试过删除顶部的第一个echo以及$error消息。 Like I said, I'm trying to improve on some code I picked up from this O'Reilly book , so I appreciate any help, guidance, criticism, etc. 就像我说的那样,我正在尝试改进从这本O'Reilly书籍中获得的代码,因此,我感谢任何帮助,指导,批评等。

Any output (echo) from the PHP script prior to setting the Location header will cause the redirection to fail. 在设置Location标头之前,PHP脚本的任何输出(回显)都将导致重定向失败。 In an ideal world, a script that redirects a user shouldn't really have a body, though many still do. 在理想的情况下,重定向用户的脚本实际上不应具有主体,尽管许多主体仍具有主体。

Remove any echo lines before you call Header. 在调用Header之前,请删除所有回声线。

You have output before your redirect. 您在重定向之前已输出。 You can't redirect once output has been sent to the browser. 将输出发送到浏览器后,您将无法重定向。

echo "<div class='main'><h3>Please enter your details to log in</h3>";

Move this code block to after your redirect code. 将此代码块移动到重定向代码之后。

您要么必须删除在标头函数之前生成任何html的代码,要么尝试将ob_start()放在页面的开头(最好是第一行)。

As already said by Aeaex , the echo will make your header redirect fail. 正如Aeaex所说,回声将使标头重定向失败。 As an alternative to using a header redirect, you could echo this : 作为使用标头重定向的替代方法,您可以显示以下内容:

'<meta http-equiv="refresh" content="2;url=http://myurl.com">';

where the number 2 in the content is the amount of seconds before the redirect, set it to 0 if it needs to be done immediately. 其中内容中的数字2是重定向之前的秒数,如果需要立即完成,请将其设置为0。

This might be bad practice and break w3 validation though. 这可能是不好的做法,但会破坏w3验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM