PHP 错误:警告:无法修改标头信息 - 标头已由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12448860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:36:13  来源:igfitidea点击:

PHP Error : Warning: Cannot modify header information - headers already sent by

php

提问by Prasanta Baidya

Plese Help me : when I lock LOGIN button, show the error, I can not fix this proplem. i am new in php.

请帮助我:当我锁定登录按钮时,显示错误,我无法修复此问题。我是 php 新手。

Warning: Cannot modify header information - headers already sent by (output started at /home/kanak/public_html/celltopc/login.php:33) in /home/kanak/public_html/celltopc/login.php on line 56

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

My code is :

我的代码是:

  <!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>

回答by Michael Hampton

You sent HTML before calling ob_start(). So when PHP tries to set your session cookie, it can't, and you get the warning.

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

To fix it, move the PHP code for ob_start()to the very top of the file:

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

<?php
ob_start();
session_start();
?>
<!DOCTYPE....

回答by Kao

You need to start the session using session_start(), before any output.

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

Session_start

会话开始

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

Alternatively you can use an output buffer, like you're already trying to. This will buffer anything between ob_start() and ob_flush()

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

Output Buffer

输出缓冲器

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