Link to home
Start Free TrialLog in
Avatar of visionarydesign
visionarydesign

asked on

Cannot modify header information - headers already sent by

hi

I am using a chat script within a session based program and I keep getting the following error:

Warning: Cannot modify header information - headers already sent by (output started at /home/visionarydesign_group/projectx.visionarydesign.co.uk/user/htdocs/beta_2/chat/php121sendim.php:15) in /home/visionarydesign_group/projectx.visionarydesign.co.uk/user/htdocs/beta_2/chat/php121sendim.php on line 61

I have had this running on a different server and worked fine but now whenever I send a chat invite I keep getting this error, below is the code I am using:

<?php
require_once("php121.php");
$sid=$HTTP_GET_VARS['sid'];

?>
                                                                                                                           
<html>
<head>
<meta http-equiv="refresh" content="5">
<title>Send IM</title>
</head>
<body>
<font style="FONT-FAMILY: Verdana,Helvetica; FONT-SIZE: 12px">

<?php
$sql = "SELECT agentid FROM session WHERE id='$sid'";
$result = mysql_query($sql,$db);
$row = mysql_fetch_row($result);
$uid = $row[0];

$sql = "select username from users where agentid = '$uid'";
$row = mysql_fetch_row(mysql_query($sql,$db));
$uname=$row[0];

$to_user = $HTTP_GET_VARS['to'];

//trying to send request to self?
if ($uid == $to_user) {
      //numpty
      echo "You can't send an IM to yourself!";
} else {
      //find out if theres already a request sent, if not, send it
      $sqla = "select r_id,r_time,r_result from ".$prefix."_requests where r_to='$to_user' and r_from='$uid'";
      $resulta = mysql_query($sqla,$db);
      $numrows = mysql_num_rows($resulta);
      $rowa = mysql_fetch_row($resulta);
      $r_id = $rowa[0];
      if ($numrows==0) {
            $sql = "insert into ".$prefix."_requests values('','$to_user','$uid',".time().",0)";
            $result = mysql_query($sql,$db);
            $sql = "select r_id from ".$prefix."_requests where r_to='$to_user' and r_from='$uid'";
            $result = mysql_query($sql,$db);
            $row = mysql_fetch_row($result);
            $r_id=$row[0];
      }
      //see if we've been accepted and that the receipiant is already in room
      $sql3 = "select c_u1_in from ".$prefix."_sessions where c_id='$r_id'";
      $result3=mysql_query($sql3,$db);
      $row3=mysql_fetch_row($result3);
      if ($row3[0]>0 && $rowa[2]==1) {
            //accepted
            $url = "Location: php121chat.php?r=".$r_id."&rc=1&sid=$sid";
            Header($url);
      }

      //see if we've been waiting too long or been rejected
      if ($numrows!=0 && (time()-$rowa[1]>60 ) ) {
            //delete request and stop refreshing
            $sql = "delete from ".$prefix."_requests where r_to='$to_user' and r_from='$uid'";
            $result = mysql_query($sql,$db);
            Header("Location: php121im-timeout.php");
      } else if ($rowa[2]==2 ){
            $sql = "delete from ".$prefix."_requests where r_to='$to_user' and r_from='$uid'";
                $result = mysql_query($sql,$db);
                Header("Location: php121im-na.php");
      } else {
            $sql = "select username from users where agentid='$to_user'";
            $result = mysql_query($sql,$db);
            $row = mysql_fetch_row($result);
            echo "Request sent to $row[0], waiting for a response...<br><br>
            <form action=\"php121cancelim.php?sid=$sid\" method=\"POST\">
            <input type=\"hidden\" name=\"to\" value=$to_user>
            <input type=\"submit\" name=\"cancel\" style=\"FONT-FAMILY: Verdana,Helvetica; FONT-SIZE: 12px\" value=\"Cancel?\"></form>";
      }
}
?>

</font>
</body>
</html>
Avatar of norcalsol
norcalsol

The error means that headers cannot be writte (e.g. Header("Location...)) after output has been sent to the web server. Couple ways to handle this -

1. turn on output buffering - e.g. ob_start at the start of the script, or in the php.ini file (if you have access to it).  This allows php erase any output it was going to send so that it can send the headers.

2. re-write the script so that if you are going to do a redirect, the header is the first and only thing sent to the output.  html blocks count as output, so you would want to reposition the header calls to the start of the script.
Also, make sure there is no whitespace before your code.
(i.e spaces, empty lines, that sort of stuff)

Because that will be considered output as well.
Avatar of blue_hunter
you already have your <html> tag printed, before header();
that's why you have the error
please make sure do not have any echo and space before the header(); in your php code.
place the following at the very top of your document:-


ob_start();
ASKER CERTIFIED SOLUTION
Avatar of Muhammad Wasif
Muhammad Wasif
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial