0

I have a PHP code . when i run the code , it works properly but there appears some warnings , that is -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

what can i do ??? ,my code is given below..

 <?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-->  
Sibu
  • 4,609
  • 2
  • 26
  • 38
  • Do any of the scripts you include make use of header()? – rws907 Nov 07 '12 at 06:10
  • possible duplicate of [Warning: Cannot modify header information - headers already sent (PHP)](http://stackoverflow.com/questions/4173740/warning-cannot-modify-header-information-headers-already-sent-php) – Richard JP Le Guen Nov 07 '12 at 06:12

8 Answers8

3

There are many solutions to this problem!!!!

1)header() must be before anything else, If header is in any included file then include it at top of page and write session_start in that page containing header.....

2)Use ob_start() at start of page, It will store the output of page in buffer and then output every thing at same time, also use ob_end_flush() at end...

3)Remember to remove empty spaces and echo statements if any before header command

4)Move include("nav_class.php"); $obj=new navig(); just after

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

and in config.php Make sure that there is no echo statement

e.g echo "Database Connection Successful";

Remove comments as well

Also remove or die ("sorry".$pic); in query

Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
Umair Aziz
  • 1,518
  • 1
  • 19
  • 29
1

use ob_start() at the start of your script and ob_end_flush() at the end.

Reference

swapnesh
  • 26,318
  • 22
  • 94
  • 126
  • 2
    that leads to performance degradation..and what is the need here?? – Arun Killu Nov 07 '12 at 06:11
  • @ArunKillu It is one out of many solutions. Cannot modify header problem can easily be googled but i think the user is the one who decides for the quick solution or the optimized one. I agree with the point. performance degradation -> Let user decide it then look at the code line by line to find what is outputting before :) – swapnesh Nov 07 '12 at 06:18
  • i just pointed out because ..he will get a wrong picture that ,when ever he gets this error he will definitely use this ,which is not supposed to be used. – Arun Killu Nov 07 '12 at 06:25
1

Ugly, ugly code....

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

...
Luke Stevenson
  • 10,357
  • 2
  • 26
  • 41
0

i dont recommend hide warning better you fix it you can use this to hide warning...

error_reporting(E_ERROR | E_PARSE);
Arun Killu
  • 13,581
  • 5
  • 34
  • 61
0

Your codes contain errors. I cannot see the contents of included files, but the error message you posted is that, you try to start a session when a session is already started.

Look for session_start() in your codes and do not echo things before header()

Raptor
  • 53,206
  • 45
  • 230
  • 366
0

Don't echo any data until after you call header(), and you won't have this problem.

slashingweapon
  • 11,007
  • 4
  • 31
  • 50
0

Your nav_class.php is outputting something at line 119, and possibly after that. You are calling header on line 32 of this script:

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

There are a few remedies:

  • Don't output anything before calling header
  • call header before including nav_class.php and creating $obj (you're not using $obj before your header call anyway)
  • As swapnesh suggested, use ob_start() and ob_end_flush().

I would favor one of the first two solutions.

Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
sonofagun
  • 355
  • 2
  • 7
0

Don't echo or print_r() after header load and if you really want to disable all errors then check index.php file if you are using codeigniter then you can unset all error messages.. there are there option and set

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.');
    }
}

just check your php.ini and then check which type of error you want to and set only one parameter and replace error_reporting(E_ALL); in this function you can replace your parameter

Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
Bhavik Patel
  • 613
  • 3
  • 11
  • 28