【问题标题】:Ignore PHP Warnings忽略 PHP 警告
【发布时间】:2012-11-07 06:08:33
【问题描述】:

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

【问题讨论】:

标签: php warnings


【解决方案1】:

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

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

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

3)如果在头命令之前记得删除空格和回显语句

4) 在

之后移动 include("nav_class.php"); $obj=new navig(); 别的 { header("位置:index.php?er=3"); } 包括(“nav_class.php”); $obj=新导航();

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

e.g echo "数据库连接成功";

同时删除 cmets

在查询中也删除 or die ("sorry".$pic);

【讨论】:

    【解决方案2】:

    丑陋,丑陋的代码....

    <?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】:

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

      Reference

      【讨论】:

      • 这会导致性能下降..这里需要什么??
      • @ArunKillu 这是众多解决方案中的一种。无法修改标题问题很容易被谷歌搜索,但我认为用户是决定快速解决方案或优化解决方案的人。我同意这一点。性能下降 -> 让用户决定然后逐行查看代码以查找之前输出的内容:)
      • 我只是指出,因为..他会得到一个错误的图片,当他得到这个错误时,他肯定会使用这个,这是不应该使用的。
      【解决方案4】:

      我不建议更好地隐藏警告你修复它 您可以使用它来隐藏警告...

      error_reporting(E_ERROR | E_PARSE);
      

      【讨论】:

      • 为什么是-ve?请发表评论,以便我可以更好地回答。
      【解决方案5】:

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

      在你的代码中寻找session_start(),不要在header()之前写echo

      【讨论】:

        【解决方案6】:

        在调用header() 之前不要回显任何数据,这样就不会出现这个问题。

        【讨论】:

          【解决方案7】:

          不要在标头加载后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); 您可以替换您的参数

          【讨论】:

            【解决方案8】:

            你的 nav_class.php 在第 119 行输出一些东西,可能在那之后。您正在此脚本的第 32 行调用标题:

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

            有一些补救措施:

            • 在调用 header 之前不要输出任何东西
            • 在包含 nav_class.php 和创建 $obj 之前调用标头 (无论如何,您在标头调用之前都没有使用$obj
            • 按照 swapnesh 的建议,使用 ob_start()ob_end_flush()

            我倾向于前两种解决方案之一。

            【讨论】:

              猜你喜欢
              • 2012-01-14
              • 1970-01-01
              • 2015-03-10
              • 2010-10-07
              • 2018-03-24
              • 2021-01-15
              • 2018-12-09
              • 2017-05-01
              • 2015-01-29
              相关资源
              最近更新 更多