2012-02-11 47 views
0

我收到Warning: Cannot modify header information。在WAMP上測試源代碼時,我沒有收到這個錯誤。在某些PHP中也包含某些類,並根據需要調用操作html的函數。任何一個人都可以指向正確的方向嗎?這是確切的錯誤和引用的文件。警告:無法修改GoDaddy上的標題信息,但不能修改

警告:不能更改頭信息 - 已經 發送頭(輸出開始 /home/content/39/8810539/html/pagoda/view/header.php:2)在 /家庭/內容/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> 
    .... 
    .... 
    .... 
+0

您可以具有之前' 2012-02-11 05:39:14

+3

這裏有點尷尬,但如果我是你,我的第一筆生意將從可怕的GoDaddy託管服務中轉移。 – rdlowrey 2012-02-11 05:40:00

+0

請刪除php文件中的尾部空格。 – Avinash 2012-02-11 05:47:15

回答

0

我用ob_start()解決了它。 ob_start首先將html寫入緩衝區,然後在頭部更改後將其寫入瀏覽器。 ob_start在你的html代碼之前。在ob_end_flush()的header()之後關閉緩衝區並不是必須的,但是這是一個很好的習慣。

的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(); 
    } 


?> 
0

BTW代碼中的一些錯誤:「?>」

1)不要使用,否則你會得到同樣的警告,如果你將不得不在文件的最後空間(有些編輯做到這一點)

2)你忘了HTML轉義 「< PHP的echo $會話級>的getName()...?」:使用用htmlspecialchars()

3)不要使用全局變量。決不。

4)邏輯和視圖應該分開。

+0

我想''''''''login.php'可能只是解決你的問題。顯然,該文件末尾有一個空格,並且刪除'?>'一個可接受的解決方案。 – seanbreeden 2012-02-11 16:25:26

+0

是否「不要使用全局變量,永遠不變」。意味着你完全不使用'$ _POST'和'$ _GET'和'$ _SERVER'編寫web應用程序?或者說,這是關於避免*全部*成本的語言特性的模棱兩可,特別是它們有意義的地方? – mario 2012-02-11 23:23:01

+0

^哈哈哈。是的.. – 2012-02-12 02:32:44

相關問題