duanchan5458 2012-10-25 18:28
浏览 48
已采纳

已发送PHP登录脚本标头[重复]

Possible Duplicate:
Headers already sent by PHP

I am trying to make a login script. This is what I have so far, but it returns this when I try to login with a valid User:

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

Here is my code:

<?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>
  • 写回答

2条回答 默认 最新

  • doujing3896 2012-10-25 18:31
    关注

    You need to do all you session handling before you start outputting HTML to the browser. Basically, you just need to move all your PHP to the beginning of the code and then handle the login success/failure messaging conditionally within your HTML output content.

    You should probably get in the habit of doing most of your code logic up front in your script and then only having minimal amount of PHP within the area of your script where you output the content.

    Eventually, as you become more experienced, you will probably also begin to want to separate out your script logic from your output entirely (i.e. place in different files altogether). Over time, most find this approach (i.e. separation of application logic and display logic) leads to code that is much easier to maintain.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器