0

好的,这是一个简单的问题,也是一个愚蠢的问题,但由于我一直在 localhost 上进行开发,我什至没有意识到这是一个问题。登录脚本中重定向到成员页面的标题不起作用,因为它的位置不正确。这在 wamp 上没有引起问题,但在实时服务器上。该脚本包含在一个 html 文件中,原因太长,无法在此处说明。脚本中没有任何内容,直到出现问题,然后脚本停止。我应该把重定向标头放在哪里?

这是登录脚本:

<?php 

// Connects to your Database 

include ("database.php");


 //Checks if there is a login cookie

 if(isset($_SESSION['username']))


 //if there is, it logs you in and directes you to the members page

 {  
    echo '<div id="probwarn"><t1><b>You are already logged in! You do not need to do it again.</b></t1></div>';

}


 //if the login form is submitted 

 if (isset($_POST['submit'])) { // if form has been submitted

$flag = 0; // Safety net, if this gets to 1 at any point in the process, we don't upload.

 // makes sure they filled it in

if(!$_POST['username'] | !$_POST['pass']) {

    echo('<div id="probwarn"><t1>You did not fill in a required field.</t1></div>');
$flag = $flag + 1;
    }

// checks it against the database





    $pass = htmlspecialchars(mysql_real_escape_string($_POST['pass']));
    $username = htmlspecialchars(mysql_real_escape_string($_POST['username']));




   $check = mysql_query("SELECT * FROM members WHERE username = '".$username."'")or die(mysql_error());



 //Gives error if user dosen't exist

 $check2 = mysql_num_rows($check);

 if ($check2 == 0) {
if($flag == 0) {
    echo('<div id="probwarn"><t1>You must <a href="register.html">    <b>register</b></a> first.</t1></div>') ;


            }
    $flag = $flag + 1;
    }           

 $check = mysql_query("SELECT * FROM members WHERE username = '".$username."'")or     die(mysql_error());
 $pass = htmlspecialchars(mysql_real_escape_string($_POST['pass']));
    $username = htmlspecialchars(mysql_real_escape_string($_POST['username']));

 while($info = mysql_fetch_array( $check ))     

 {

 $_POST['pass'] = stripslashes($_POST['pass']);

$info['password'] = stripslashes($info['password']);

$_POST['pass'] = md5($_POST['pass']);



 //gives error if the password is wrong

if ($_POST['pass'] != $info['password']) {
 if($flag == 0) {
    echo('<div id="probwarn"><t1>Incorrect password, please try again.</t1>    </div>');
$flag ++;
     } }

   }        





 // if login is ok then we add a cookie 
 if($flag == 0) {
 $pass = htmlspecialchars(mysql_real_escape_string($_POST['pass']));
 $username = htmlspecialchars(mysql_real_escape_string($_POST['username']));



 $_SESSION['username']=$username;
 $_SESSION['password']=$pass;

 //then redirect them to the members area
//THIS IS THE HEADER 
 header("Location: ../members.html");





 } 

 } 

 else 





 // if they are not logged in 

 ?> 
 LOGIN FORM IS USUALLY HERE
4

2 回答 2

3

exit您在设置 Location 标头后忘记了。除非您确切地知道自己在做什么,否则您应该始终将exit(或die()) 放在 Location 标头之后。

于 2012-06-22T16:26:04.620 回答
0

您可以在代码的任何地方使用 header('Location: address'),但最好在 header 之后使用 exit() 函数,如果未执行重定向,则其他代码不会执行。

于 2012-06-22T16:49:45.050 回答