Freitag, 22. August 2014

PHP Warning: Cannot modify header information - headers already sent by (output started at x ) in x on line x, referer: x

 Since i stumbled accross this while writing some stuff in php

The code i had before was this:


[code]

                if(@$edit=="yes2" & $username==$name['name']){
                $chadesc = $desc['beschreibung'];
                $chadesc=ereg_replace ("<br>", "\n", $chadesc);
                echo('<td width="900" height="251" style="background-image:url(images/mitte.png)"><form action="portal.php?page=videos&id='.$id.'&video='.$video.'&edit=yes3&update2=yes" method="post"><div align="center"><textarea type="text" cols="95%" rows="10" name="kanaldesc">'.$chadesc.'</textarea><div align="center"><input type="Submit" value="Update"></div></form></div></td>');
                }else{
                echo('<td width="900" height="251" style="background-image:url(images/mitte.png)">'.$desc['beschreibung'].'</td>');
                }
               
               
               
                echo('<td width="51" height="251" style="background-image:url(images/mitterechts.png)"></td>
                </tr>
                <tr>
                <td width="51" height="51" style="background-image:url(images/eckeulinks.png)"></td>
                <td width="900" height="51" style="background-image:url(images/mitteunten.png)"></td>
                <td width="51" height="51" style="background-image:url(images/eckeurechts.png)"></td>
                </tr>
                </table>
                <br><br>
               
               
                </div>');
       
                if(@$update2=="yes" & $edit=="yes3" & @$id!="" & $video!=""){
                @$chdesc = $_POST['kanaldesc'];
                $chdesc=ereg_replace ("\n", "<br>", $chdesc);
                $updateheader2 = ('UPDATE video set beschreibung="'.$chdesc.'" where id="'.$id.'" and videoname="'.$video.'"');
                $uphead2 = mysql_query($updateheader2);
                $desc= mysql_fetch_array($beschreibung);
                header('Location: http://10.49.30.81/portal.php?page=videos&id='.$id.'&video='.$video);
                }


[/code]


You can see the code below the <form> and the if at the bottom which is an echo with text.

if(@$edit=="yes2" & $username==$name['name']){
                $chadesc = $desc['beschreibung'];
                $chadesc=ereg_replace ("<br>", "\n", $chadesc);
                echo('<td width="900" height="251" style="background-image:url(images/mitte.png)"><form action="portal.php?page=videos&id='.$id.'&video='.$video.'&edit=yes3&update2=yes" method="post"><div align="center"><textarea type="text" cols="95%" rows="10" name="kanaldesc">'.$chadesc.'</textarea><div align="center"><input type="Submit" value="Update"></div></form></div></td>');


 if(@$update2=="yes" & $edit=="yes3" & @$id!="" & $video!=""){


 I received the above error. Even though i had almost the same a few lines higher which worked pretty well i received errors only in my /var/log/apache2/error.log

The error.log pointed to either the beginning of the echo or the last line of it.

After thinking about it since i had the same problem some month ago when i made the first function which now works i remembered. 

The solution is putting the second if with the header directly beyond the <form> with NO echo between it.

So now my code looks like this and works well.



 if(@$edit=="yes2" & $username==$name['name']){
                $chadesc = $desc['beschreibung'];
                $chadesc=ereg_replace ("<br>", "\n", $chadesc);
                echo('<td width="900" height="251" style="background-image:url(images/mitte.png)"><form action="portal.php?page=videos&id='.$id.'&video='.$video.'&edit=yes3&update2=yes" method="post"><div align="center"><textarea type="text" cols="95%" rows="10" name="kanaldesc">'.$chadesc.'</textarea><div align="center"><input type="Submit" value="Update"></div></form></div></td>');
                }else{
                echo('<td width="900" height="251" style="background-image:url(images/mitte.png)">'.$desc['beschreibung'].'</td>');
                }
               
               
                if(@$update2=="yes" & $edit=="yes3" & @$id!="" & $video!=""){
                @$chdesc = $_POST['kanaldesc'];
                $chdesc=ereg_replace ("\n", "<br>", $chdesc);
                $updateheader2 = ('UPDATE video set beschreibung="'.$chdesc.'" where id="'.$id.'" and videoname="'.$video.'"');
                $uphead2 = mysql_query($updateheader2);
                $desc= mysql_fetch_array($beschreibung);
                header('Location: http://10.49.30.81/portal.php?page=videos&id='.$id.'&video='.$video);
                }
               
               
                echo('<td width="51" height="251" style="background-image:url(images/mitterechts.png)"></td>
                </tr>
                <tr>
                <td width="51" height="51" style="background-image:url(images/eckeulinks.png)"></td>
                <td width="900" height="51" style="background-image:url(images/mitteunten.png)"></td>
                <td width="51" height="51" style="background-image:url(images/eckeurechts.png)"></td>
                </tr>
                </table>
                <br><br>
               
               
                </div>');

        


Hope this saved you some hassle.

Invi