1

请帮助我:当我锁定登录按钮时,显示错误,我无法解决这个问题。我是 php 新手。

警告:无法修改标头信息 - 标头已由第 56 行 /home/kanak/public_html/celltopc/login.php 中的(输出开始于 /home/kanak/public_html/celltopc/login.php:33)发送

我的代码是:

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
<link href="css/usercss.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body {
    background-image: url(images/LoginForm_bg.jpg);
    background-repeat: repeat;
}
-->
</style></head>

<body>
<div class="shadow" id="login">
  <div id="login-body">
  <form action="login.php" method="post" name="loginform">
    <strong>Username:</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input name="username" type="text" class="input" value="username" maxlength="10" />

    <p><br />
  <strong>Password:</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  <input name="password" type="password" class="input" value="password" />

  <p><br />
   <input type="submit" name="submit" value="Login" width="30" class="myButton">

  </form>

   <p> <br />
<?php
ob_start();
@session_start();
include'include/config.php';
include'include/opendb.php';
@$username=$_POST['username'];
@$password=$_POST['password'];

    if($username&&$password)
    {
    $connect=mysql_connect("$dbhost","$dbuser","$dbpass")or die("Couldnt connect to Database");
    mysql_select_db("$dbname") or die ("Could not find to Database");
    $query = mysql_query ("SELECT * FROM user WHERE username='$username'");
    $numrows = mysql_num_rows($query); 
    if ($numrows != 0)
        {
        while ($rows = mysql_fetch_assoc($query))
            {
            $dbusername = $rows['username'];
            $dbpassword = $rows['password'];
            }
                if($username==$dbusername&&$password==$dbpassword)
                {
header("location:main.php");

$_SESSION['username']=$dbusername;
                }
                else        
                die("<p class=error>*&nbsp;Increate Password</p>");     

        }
        else die("<p class=error>*&nbsp;The username doesnot exist</p>");
    }
    else die ("<p class=text>*&nbsp;Please entre a username and password</p>");
?>
</div>
</div>

</body>
</html>
4

2 回答 2

18

您在调用之前发送了 HTML ob_start()。因此,当 PHP 尝试设置会话 cookie 时,它​​不能,并且您会收到警告。

要修复它,请将 PHP 代码移动ob_start()到文件的最顶部:

<?php
ob_start();
session_start();
?>
<!DOCTYPE....
于 2012-09-14T07:03:07.193 回答
8

您需要在任何输出之前使用 session_start() 启动会话。

会话开始

<?php
    session_start();
?>
<html>
   <!-- Output -->
</html>

或者,您可以使用输出缓冲区,就像您已经尝试过的那样。这将缓冲 ob_start() 和 ob_flush() 之间的任何内容

输出缓冲器

<?php
    ob_start();
?>
<html>
    <!-- Output -->
</html>
<?php
    session_start();
    ob_flush();
?>
于 2012-09-14T07:03:18.180 回答