2012-11-07 56 views
0

我有一個PHP代碼。當我運行的代碼,它的工作正常,但出現一些警告,那是 - Warning: Cannot modify header information - headers already sent by (output started at /home/fundumob/public_html/app_create/nav_class.php:119) in /home/fundumob/public_html/app_create/profile.php on line 32忽略PHP警告

我該怎麼辦? ,我的代碼如下..

<?php session_start(); error_reporting(E_ALL^E_WARNING);?> 
<?php 
    //include("local_config.php"); 
    include("lib/config.php"); 
    include("nav_class.php"); 
    $obj=new navig(); 

    if(isset($_SESSION['user_id'])) 
    { 
    $user_id=$_SESSION['user_id']; 
    $pic="select Picture from Profile where user_id=$user_id "; 
    $pic_result=mysql_query($pic) or die ("sorry".$pic); 
    $count=mysql_num_fields($pic_result); 

    if($count==1) 
    { 

    $rows=mysql_fetch_row($pic_result); 

    $_SESSION['picture']=$rows[0]; 
    $profile_pic=$_SESSION['picture']; 

    $photo="profile/".$profile_pic; 
    } 
    else 
    { 
    $photo="profile/img.jpg"; 
    } 
    } 
    else 
    { 
    header("location:index.php?er=3"); 
    } 

    ?> 

    <div id="templatemo_header_wrapper"> 

<div id="templatemo_header"> 

    <div id="site_logo"></div> 


     <div id="menu_nav" > 
    <ul id="css3menu1" class="topmenu"> 
    <li class="topfirst"><a href="profile.php?apps=1" style="height:15px;line- 
    height:15px;">Dashboard</a></li> 
    <li class="topmenu"><a href="#" style="height:15px;line-height:15px;">Profile</a>  
     </li> 
    <li class="topmenu"><a href="#" style="height:15px;line-height:15px;">Settings</a> 
    </li> 
    <li class="toplast"><a href="#" style="height:15px;line-height:15px;">Prateek  

</ul> 
</div> </div><!-- end of header --> 
</div> 

    <div id="tempatemo_content_wrapper"> 

    <div id="templatemo_content" align="center"> 

    <div class="recent_projects"> 

      <div align="right"> 
      <table width="50%" border="0"> 
      <tr> 
      <td width="85%" height="20" style="border-right:1px solid #D0D0D0;"><div  
     align="right">Welcome&nbsp;&nbsp;&nbsp;<?php echo $_SESSION['fname']."&nbsp;". 
     $_SESSION['lname']; ?></div></td> 
      <td width="15%" > <div align="center"><a href="logout.php">Log Out</a></div> 
      </td> 
      </tr> 
      </table> 
      </div> 

    </div> 
    <!--end of recent project--> 
+0

是否有任何腳本的你包括使用標題()? – rws907

+0

可能的重複[警告:不能修改標題信息 - 標題已發送(PHP)](http://stackoverflow.com/questions/4173740/warning-cannot-modify-header-information-headers-already-sent-php) –

回答

1

醜陋的,醜陋的代碼....

<?php 

@session_start(); 
error_reporting(E_ALL^E_WARNING); 

//include("local_config.php"); 
include('lib/config.php'); 
include('nav_class.php'); 

$obj = new navig(); 

if(!isset($_SESSION['user_id'])){ 

    if(!headers_sent()) 
    header('Location: index.php?er=3'); 
    echo '<script type="text/javascript">document.location.href="index.php?er=3";</script>'; 
    die(); 

} 

$photo = 'profile/img.jpg'; 

if(!isset($_SESSION['picture'])){ 

    $user_id = (int) $_SESSION['user_id']; 
    $pic = "SELECT Picture FROM Profile WHERE user_id=$user_id"; 

    if(($pic_result = mysql_query($pic)) && mysql_num_rows($pic_result)==1){ 
    $row = mysql_fetch_row($pic_result); 
    $_SESSION['picture'] = $row[0]; 
    $photo = 'profile/'.$row[0]; 
    } 

}else{ 

    $photo = 'profile/'.$_SESSION['picture']; 

} 

?> 

<div id="templatemo_header_wrapper"> 
    <div id="templatemo_header"> 
    <div id="site_logo"></div> 
    <div id="menu_nav"> 
     <ul id="css3menu1" class="topmenu"> 
     <li class="topfirst"><a href="profile.php?apps=1" style="height:15px;line-height:15px;">Dashboard</a></li> 
     <li class="topmenu"><a href="#" style="height:15px;line-height:15px;">Profile</a></li> 
     <li class="topmenu"><a href="#" style="height:15px;line-height:15px;">Settings</a></li> 
     <li class="toplast"><a href="#" style="height:15px;line-height:15px;">Prateek</li> 
     </ul> 
    </div> 
    </div><!-- end of header --> 
</div> 

<div id="tempatemo_content_wrapper"> 
    <div id="templatemo_content" align="center"> 
    <div class="recent_projects"> 
     <div align="right"> 
     <table width="50%" border="0"> 
      <tr> 
      <td width="85%" height="20" style="border-right:1px solid #D0D0D0;"><div align="right">Welcome&nbsp;&nbsp;&nbsp;<?php echo $_SESSION['fname'].'&nbsp;'.$_SESSION['lname']; ?></div></td> 
      <td width="15%" ><div align="center"><a href="logout.php">Log Out</a></div></td> 
      </tr> 
     </table> 
     </div> 
    </div> 
    <!--end of recent project--> 

... 
1

使用ob_start()在腳本的開始和ob_end_flush()末。

Reference

+2

導致性能下降..這裏有什麼需要? –

+0

@ArunKillu這是許多解決方案之一。無法修改標題問題可以很容易地搜索到,但我認爲用戶是決定快速解決方案還是優化解決方案的用戶。我同意這一點。性能下降 - >讓用戶決定,然後逐行查看代碼,找到之前輸出的內容:) – swapnesh

+0

我剛纔指出,因爲..他會得到一個錯誤的圖片,當他得到這個錯誤時,他肯定會使用這是不應該使用的。 –

0

我不建議隱藏警告你更好解決它 你可以使用這個隱藏警告...

error_reporting(E_ERROR | E_PARSE); 
+1

why -ve?請發表評論,以便我可以更好地回答我的答案。 –

0

你的代碼包含錯誤。我看不到包含文件的內容,但是您發佈的錯誤消息是,您嘗試在會話已啓動時開始會話。

尋找在你的代碼session_start(),做之前不echo東西header()

0

不迴應任何數據,直到您叫header()後,你會不會有這個問題。

0

您的nav_class.php正在輸出119行的內容,並可能在此之後。

header("location:index.php?er=3"); 

有一些補救措施:

  • 請致電頭之前不能輸出任何東西
  • 包括nav_class.php和創建
  • 電話頭之前,你對這個腳本的第32行調用頭$obj (在你的頭文件調用之前,你沒有使用$obj
  • 正如swapnesh建議的那樣,使用ob_start()ob_end_flush()

我喜歡前兩種解決方案之一。

3

對於這個問題,許多解決方案!!!!

1)header()必須先天下之憂,如果標題是包含任何頁面頂部的文件,然後包括它寫在包含頁頭session_start .....

2)在開始使用ob_start()它將頁面的輸出存儲在緩衝區中,然後同時輸出每一樣東西,最後也使用ob_end_flush() ...

3)記住要刪除空的空間和回聲陳述,如果剛過

 
    else 
    { 
     header("location:index.php?er=3"); 
    } 
    include("nav_class.php"); 
    $obj=new navig(); 

並在config.php確保沒有迴音聲明任何頭命令

4)之前將include("nav_class.php"); $obj=new navig();

eg echo「Database Connection Successful」;

刪除評論以及

同時刪除或死亡( 「對不起」 $ PIC);查詢

0

不要echoprint_r()頭負荷後,如果你真的要禁用所有的錯誤,然後檢查index.php文件,如果你使用的是笨,那麼你可以不設置所有錯誤消息.. 有沒有選項並設置

if (defined('ENVIRONMENT')) 
{ 
    switch (ENVIRONMENT) 
    { 
     case 'development': 
      error_reporting(E_ALL); 
     break; 

     case 'testing': 
     case 'production': 
      error_reporting(0); 
     break; 

     default: 
      exit('The application environment is not set correctly.'); 
    } 
} 

只是檢查你的php.ini,然後檢查你要哪種類型的錯誤,並只設定一個參數,並在這個函數替換error_reporting(E_ALL);可以取代你的參數