-2

可能重复:
PHP 已发送的标头

我正在尝试制作登录脚本。这是我到目前为止所拥有的,但是当我尝试使用有效用户登录时它会返回:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/user/public_html/dir/login.php:3) in /home/user/public_html/dir/login.php on line 6

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/user/public_html/dir/login.php:3) in /home/user/public_html/dir/login.php on line 6

Warning: Cannot modify header information - headers already sent by (output started at /home/user/public_html/dir/login.php:3) in /home/user/public_html/dir/login.php on line 31

这是我的代码:

<?php
if(isset($_POST['login'])){
ob_start();
session_start();
$host="xxx";
$username_db="xxx"; 
$password_db="xxx";
$db_name="xxx";  
$tbl_name="xxx"; 

mysql_connect("$host", "$username_db", "$password_db")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$email=$_POST['email']; 
$password=$_POST['password']; 

$email = stripslashes($email);
$password = stripslashes($password);
$email = mysql_real_escape_string($email);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM $tbl_name WHERE username='$email' and password='$password'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){
$_SESSION['username'] = $email;
$_SESSION['password'] = $password;
header("location:main.php");
}
else {
echo"<br><center><div class=\"alert alert-error\">
Wrong email or password!</div></center>";

}
ob_end_flush();
}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Login</title>
    <meta name="description" content="">
    <meta name="author" content="">

    <link href="bootstrap/css/bootstrap.css" rel="stylesheet">
    <style type="text/css">
      html, body {
        background-color: #eee;


      }
      body {
        padding-top: 40px; 
      }
      .container {
        width: 300px;
      }

      .container > .content {
        background-color: #fff;
        padding: 20px;
        margin: 0 -20px; 
        -webkit-border-radius: 10px 10px 10px 10px;
           -moz-border-radius: 10px 10px 10px 10px;
                border-radius: 10px 10px 10px 10px;
        -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15);
           -moz-box-shadow: 0 1px 2px rgba(0,0,0,.15);
                box-shadow: 0 1px 2px rgba(0,0,0,.15);
      }

      .login-form {
        margin-left: 65px;
      }

      legend {
        margin-right: -50px;
        font-weight: bold;
        color: #404040;
      }

    </style>

</head>
<body>


    <div class="navbar navbar-inverse navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container">
          <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </a>
          <a class="brand" href="#">hi</a>
          <div class="nav-collapse collapse">
            <ul class="nav">
              <li class="active"><a href="#">Home</a></li>
              <li><a href="register.php">Register</a></li>
              <li><a href="contact.php">Contact</a></li>
            </ul>

          </div><!--/.nav-collapse -->
        </div>
      </div>
    </div>

    <div class="container">
        <div class="content">
            <div class="row">
                <div class="login-form">
                    <h2>Login</h2>
                    <form method="post" action="">
                        <fieldset>
                            <div class="clearfix">
                                <input name="email" type="text" placeholder="Email">
                            </div>
                            <div class="clearfix">
                                <input name="password" type="password" placeholder="Password">
                            </div>
<button name="login" class="btn btn-primary" type="submit">Sign in</button><br>

                        </fieldset>
                    </form>
                </div>
            </div>
        </div>
    </div> <!-- /container -->
</body>
</html>
4

2 回答 2

0

我看到您使用ob_start()的是不带参数的,因此您了解可以使用它来防止提前发送标头。但是,您似乎有点忽略了这一点:必须在发送任何页面内容之前调用它。在这种情况下,它应该在<!DOCTYPE.

于 2012-10-25T18:31:24.647 回答
0

在开始将 HTML 输出到浏览器之前,您需要完成所有会话处理。基本上,您只需将所有 PHP 移动到代码的开头,然后在 HTML 输出内容中有条件地处理登录成功/失败消息。

您可能应该养成在脚本中预先完成大部分代码逻辑的习惯,然后在输出内容的脚本区域内只使用最少量的 PHP。

最终,随着您变得更有经验,您可能还会开始希望将脚本逻辑与输出完全分开(即完全放在不同的文件中)。随着时间的推移,大多数人发现这种方法(即应用程序逻辑和显示逻辑的分离)导致代码更容易维护。

于 2012-10-25T18:31:36.323 回答