收到警告:无法修改 GoDaddy 上的标头信息,但不能修改 Wamp 上的标头信息

发布于 2025-01-04 17:22:18 字数 1570 浏览 2 评论 0原文

我收到警告:无法修改标头信息。在 WAMP 上测试源代码时,我没有收到此错误。此外,在某些 PHP 中还包含某些类,并根据需要调用操作 html 的函数。有人能指出我正确的方向吗?这是确切的错误和引用的文件。

警告:无法修改标头信息 - 标头已由 (输出开始于 /home/content/39/8810539/html/pagoda/view/header.php:2) 在 /home/content/39/8810539/html/pagoda/controller/login.php 第 9 行

login.php:

<?php

    global $session;
    global $view;

    if (isset($_POST['login']))
    {
        if ($session->logIn($_POST["uname"],$_POST["password"]))
            header("Location: $_SERVER[REQUEST_URI]");
        else
            $view->RenderMsg("Your username and/or password was incorrect.");
    }
    if (isset($_POST['logout']))
    {
        $session->logOut();
        header("Location: $_SERVER[REQUEST_URI]"); 
    }


?>

header.php片段:

<head>
<link rel="stylesheet" type="text/css" href="<?php echo BASEPATH; ?>/view/css/homepageStyle.css" />
</head>
<div id="header">
    <div id="banner">
        Pagoda
    </div>
    <div id="login">

<?php 

    //This controls the login/logout header on the top of each page.

    global $session;
    if ($session->isLoggedIn())
    {
?>
    <div id="nametag">
        <form method="POST" name="logout" action="">
        Welcome, <?php echo $session->getName()." (".$session->getRole().")"?>
        <input name="logout" type="hidden" value="Log Out"/>
        </form>
    </div>  
    ....
    ....
    ....

I am getting the Warning: Cannot modify header information. I am NOT getting this error when testing the source code on WAMP. Also in some of the PHP certain classes are included and functions to manipulate the html are called as needed. Can any one point me in the right direction? Here's the exact error and the cited files.

Warning: Cannot modify header information - headers already sent by
(output started at
/home/content/39/8810539/html/pagoda/view/header.php:2) in
/home/content/39/8810539/html/pagoda/controller/login.php on line 9

login.php:

<?php

    global $session;
    global $view;

    if (isset($_POST['login']))
    {
        if ($session->logIn($_POST["uname"],$_POST["password"]))
            header("Location: $_SERVER[REQUEST_URI]");
        else
            $view->RenderMsg("Your username and/or password was incorrect.");
    }
    if (isset($_POST['logout']))
    {
        $session->logOut();
        header("Location: $_SERVER[REQUEST_URI]"); 
    }


?>

header.php snippet:

<head>
<link rel="stylesheet" type="text/css" href="<?php echo BASEPATH; ?>/view/css/homepageStyle.css" />
</head>
<div id="header">
    <div id="banner">
        Pagoda
    </div>
    <div id="login">

<?php 

    //This controls the login/logout header on the top of each page.

    global $session;
    if ($session->isLoggedIn())
    {
?>
    <div id="nametag">
        <form method="POST" name="logout" action="">
        Welcome, <?php echo $session->getName()." (".$session->getRole().")"?>
        <input name="logout" type="hidden" value="Log Out"/>
        </form>
    </div>  
    ....
    ....
    ....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

预谋 2025-01-11 17:22:18

顺便说一句,您的代码中存在一些错误:

1)不要使用“?>”,否则如果文件末尾有空格,您将收到相同的警告(某些编辑器会这样做)

2)您忘记了“中的HTML转义” getName()...": 使用 htmlspecialchars()

3) 不要使用全局变量。绝不。

4)逻辑和视图应该分离。

BTW some errors in your code:

1) Do not use "?>", or you will receive the same warning if you will have spaces at the end of the files(some editors do it)

2) You forgot HTML escaping in "<?php echo $session->getName()...": use htmlspecialchars()

3) Do not use globals. Never.

4) Logic and view should be separated.

公布 2025-01-11 17:22:18

我用 ob_start() 解决了这个问题。 ob_start 首先将 html 写入缓冲区,然后在标头更改后将其写入浏览器。 ob_start 位于您的 html 代码之前。在 header() 之后使用 ob_end_flush() 关闭缓冲区不是必需的,但这是一个很好的做法;

header.php 片段:

<?php
     ob_start()
 ?>

<head>
<link rel="stylesheet" type="text/css" href="<?php echo BASEPATH; ?>/view/css/homepageStyle.css" />
</head>
<div id="header">
    <div id="banner">
        Pagoda
    </div>
    <div id="login">

<?php 

    //This controls the login/logout header on the top of each page.

    global $session;
    if ($session->isLoggedIn())
    {
?>
    <div id="nametag">
        <form method="POST" name="logout" action="">
        Welcome, <?php echo $session->getName()." (".$session->getRole().")"?>
        <input name="logout" type="hidden" value="Log Out"/>
        </form>
    </div>  
    ....
    ....
    ....

login.php

<?php

    global $session;
    global $view;

    if (isset($_POST['login']))
    {
        if ($session->logIn($_POST["uname"],$_POST["password"]))
            header("Location: $_SERVER[REQUEST_URI]");
            ob_end_flush();
        else
            $view->RenderMsg("Your username and/or password was incorrect.");
    }
    if (isset($_POST['logout']))
    {
        $session->logOut();
        header("Location: $_SERVER[REQUEST_URI]");
        ob_end_flush(); 
    }


?>

I solved it with ob_start(). ob_start writes the html to a buffer first, then it writes it to the browser after the header has changed. ob_start goes before your html code. It's not necessary but good practice to close the buffer after the header() with ob_end_flush();

header.php snippet:

<?php
     ob_start()
 ?>

<head>
<link rel="stylesheet" type="text/css" href="<?php echo BASEPATH; ?>/view/css/homepageStyle.css" />
</head>
<div id="header">
    <div id="banner">
        Pagoda
    </div>
    <div id="login">

<?php 

    //This controls the login/logout header on the top of each page.

    global $session;
    if ($session->isLoggedIn())
    {
?>
    <div id="nametag">
        <form method="POST" name="logout" action="">
        Welcome, <?php echo $session->getName()." (".$session->getRole().")"?>
        <input name="logout" type="hidden" value="Log Out"/>
        </form>
    </div>  
    ....
    ....
    ....

login.php

<?php

    global $session;
    global $view;

    if (isset($_POST['login']))
    {
        if ($session->logIn($_POST["uname"],$_POST["password"]))
            header("Location: $_SERVER[REQUEST_URI]");
            ob_end_flush();
        else
            $view->RenderMsg("Your username and/or password was incorrect.");
    }
    if (isset($_POST['logout']))
    {
        $session->logOut();
        header("Location: $_SERVER[REQUEST_URI]");
        ob_end_flush(); 
    }


?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文