忽略PHP警告

时间:2012-11-07 06:08:33

标签: php warnings

我有一个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 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-->  

8 个答案:

答案 0 :(得分:3)

这个问题有很多解决办法!!!!

1)header()必须先于其他任何内容,如果标题位于任何包含的文件中,则将其包含在页面顶部,并在包含标题的页面中写入session_start .....

2)在页面开头使用ob_start(),它会将页面输出存储在缓冲区中,然后同时输出所有内容,最后也使用ob_end_flush() ...

3)记住在header命令

之前删除空格和echo语句

4)在

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

并在config.php中确保没有echo语句

例如echo“数据库连接成功”;

删除评论

同样在查询中删除或死(“抱歉”。$ pic);

答案 1 :(得分:1)

在脚本开头使用ob_start(),在结尾使用ob_end_flush()

Reference

答案 2 :(得分: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-->

...

答案 3 :(得分:0)

我不推荐隐藏警告,你可以更好地修复它 你可以用它来隐藏警告...

error_reporting(E_ERROR | E_PARSE);

答案 4 :(得分:0)

您的代码包含错误。我看不到包含文件的内容,但是您发布的错误消息是,您尝试在会话已经启动时启动会话。

在代码中查找session_start()echo之前不要header()

答案 5 :(得分:0)

在致电header()之前,请勿回复任何数据,否则您将不会遇到此问题。

答案 6 :(得分:0)

您的nav_class.php正在第119行输出内容,之后可能会输出。您正在调用此脚本第32行的标题:

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

有一些补救措施:

  • 在调用标题
  • 之前不要输出任何内容
  • 在包含nav_class.php和创建$obj之前调用标题 (无论如何,你在标题通话之前没有使用$obj
  • 正如swapnesh建议的那样,请使用ob_start()ob_end_flush()

我赞成前两个解决方案中的一个。

答案 7 :(得分:0)

标题加载后不要echoprint_r()如果你真的想要禁用所有错误,那么检查index.php文件,如果你使用的是codeigniter,那么你可以取消设置所有错误消息。 有选项和设置

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);在这个函数中你可以替换你的参数