...

View Full Version : Headers already sent?



tribalmaniac
11-07-2006, 05:25 PM
I have a php script whereby the user inputs their information and it gets submitted to the database.
when it is submitted, I get this error:

Warning: Cannot modify header information - headers already sent by (output started at /home/.mala/tribalmaniac/theflashplace.com/top.php:16) in /home/.mala/tribalmaniac/theflashplace.com/myprofile.php on line 979

top.php is this:


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="mainStyle.css" />
<title>.:: The Flash Place ::.</title>
<link rel="stylesheet" type="text/css" href="mainstylesheet.css" />
<SCRIPT LANGUAGE="JavaScript">
extensionArray = new Array(".jpg", ".png", ".gif");
function LimitAttachImage(form, file) {
allowSubmit = false;
if (!file) return;
while (file.indexOf("\\") != -1)
file = file.slice(file.indexOf("\\") + 1);
ext = file.slice(file.indexOf(".")).toLowerCase();
for (var i = 0; i < extensionArray.length; i++) {
if (extensionArray[i] == ext) { allowSubmit = true; break; }
}
if (allowSubmit) form.submit();
else
alert("Sorry, that file type is not allowed");
}
</script>
<link href="css/screen.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript" src="/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
tinyMCE.init({
mode : "specific_textareas",
editor_selector : "tinyMCE",
theme : "advanced",
plugins : "spellchecker,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,z oom,flash,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable",
theme_advanced_buttons1_add_before : "save,newdocument,separator",
theme_advanced_buttons1_add : "fontselect,fontsizeselect",
theme_advanced_buttons2_add : "separator,insertdate,inserttime,preview,separator,forecolor,backcolor",
theme_advanced_buttons2_add_before: "cut,copy,paste,pastetext,pasteword,separator,search,replace,separator",
theme_advanced_buttons3_add_before : "tablecontrols,separator",
theme_advanced_buttons3_add : "emotions,iespell,flash,advhr,separator,print,separator,ltr,rtl,separator,fullscreen",
theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,spellchecker",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_path_location : "bottom",
plugin_insertdate_dateFormat : "%d/%m/%Y",
plugin_insertdate_timeFormat : "%H:%M:%S",
extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
external_link_list_url : "example_data/example_link_list.js",
external_image_list_url : "example_data/example_image_list.js",
flash_external_list_url : "example_data/example_flash_list.js",
force_br_newlines : "true",
content_css : "style/content.css",
theme_advanced_resize_horizontal : false,
theme_advanced_resizing : true,
// file_browser_callback : "mcFileManager.filebrowserCallBack",
convert_urls : false,
dialog_type : "modal"
});
</script>

</head>
<body>
<div id="main">
<div id="banner">
<img src="header.png" width="784" height="138" />
</div>
<div id="content">
<div id="navpos">
<p class="navtext">

<?php
include("connection.php");
putenv("TZ=Europe/London");
session_start();
$id = $_SESSION['id'];
$anynew = mysql_query("SELECT * FROM `message` WHERE `touser` = '$id' AND `read` = 0");
$anynew_rows = mysql_num_rows($anynew);

if(empty($_SESSION['id'])) {
echo('<A HREF="index2.php">Home</A> | <A HREF="news.php">News</A> | <A HREF="tutorials.php">Tutorials</A> | <A HREF="forum.php">Forum</A> | <A HREF="gallery.php">Gallery</A> | <A HREF="myprofile.php">My Profile</A> | <A HREF="messages.php">My Messages</A> | <A HREF="register.php">Register</A>');
}

else if(isset($_SESSION['id']))
{
echo('<A HREF="index2.php">Home</A> | <A HREF="news.php">News</A> | <A HREF="tutorials.php">Tutorials</A> | <A HREF="forum.php">Forum</A> | <A HREF="gallery.php">Gallery</A> | <A HREF="myprofile.php">My Profile</A> | <A HREF="messages.php">My Messages ('.$anynew_rows.' new)</A> | <A HREF="logout.php">Log Out</A>');
}

?>
</p>
</div>



I've been told this problem is caused by whitespace, but i cant see where ive gone wrong?

mlseim
11-07-2006, 06:04 PM
I think session_start() must be the very first thing on your .php page.
Do all of the session stuff at the top, before anything else.

tribalmaniac
11-07-2006, 06:22 PM
i moved all the session stuff to the top, but its made no difference?

thing is on all the other headers on all the other pages except "myprofile" work just fine

littlejones
11-07-2006, 06:54 PM
Try putting all of the php code at the very top of the page, and then ensure there is no whitespace before the first <?php tag. If that doesn't work then post on here the exact error message so we can better evaluate where the problem is coming from.

littlejones
11-07-2006, 06:57 PM
Sorry I see you already have put the error message in there, stupid me!

Have you sent any headers in the included file "connection.php" ?

If you have then that would be your problem.

tribalmaniac
11-07-2006, 07:15 PM
I put all the php at the top, and still the same error comes up, my connection page is just


<?php
$hostname = "***.theflashplace.com";
$dbusername = "***";
$dbpassword = "***";
$database = "***";

$connection = mysql_connect($hostname, $dbusername, $dbpassword) or die(mysql_error());
mysql_select_db($database, $connection) or die(mysql_error());
?>

Spookster
11-07-2006, 07:18 PM
The session_start() function can only be executed prior to anything being outputted to the web browser. The first thing in that file needs to be:



<?php
session_start();
?>
<html>
<head>
etc.


If you have outputted anything to the browser at all including whitespace you will get that error.

tribalmaniac
11-07-2006, 07:40 PM
I did that, and checked for whitespace still the same problem

It's driving me mad

Spookster
11-07-2006, 07:46 PM
Do you have any files specified in the auto_prepend setting that are getting included in every file?

tribalmaniac
11-07-2006, 07:50 PM
sorry, i dont think ive used 'auto_prepend' ?

ess
11-07-2006, 07:51 PM
Hello there.

No...this has nothing to do with session...though your session_start should always be at the top of your file.

This error is usually generated when you have already used a header("Location: nextpage.php" ); function or any function that modifies the response header of a given request.

to overcome this problem....create a .htaccess file and have the following lines written at the top of the file.

PHP_FLAG output_buffering On
php_value memory_limit "32M"

If you cannot use .htaccess file...google search how you can set the above values using init_set() function...

Good luck
:thumbsup:

GJay
11-07-2006, 07:59 PM
session_start sends headers 'under the hood', and editing .htaccess files isn't really necessary.

it's probably worth posting the current state of your code as it's difficult to keep track of you saying "i've done that but it's still broken".

tribalmaniac
11-07-2006, 08:07 PM
the top.php is still the same but with the session_start at the top of the file
I dont think it has anything to do with the top.php file because other files use the same page and also use headers and they all work fine.

I have done this:


if (($id == $id2) and ($admin == 1)){
mysql_query("UPDATE members SET `displayname` = '$displayname' WHERE `id` = '$id'");
$URL="myprofile.php?id=$id";
header ("Location: $URL");
}
else if (($id == $id2) and ($id == 7)){
mysql_query("UPDATE members SET `displayname` = '$displayname' WHERE `id` = '$id'");
$URL="myprofile.php?id=$id";
header ("Location: $URL");
}
else if($id == 7){
$URL="myprofile.php?id=$id";
header ("Location: $URL");
}
else if(($mod == 1) or ($admin == 1)){
mysql_query("UPDATE members SET `displayname` = '$displayname' WHERE `id` = '$id'");
$URL="myprofile.php?id=$id";
header ("Location: $URL");
}
else if($id == $id2){
mysql_query("UPDATE members SET `displayname` = '$displayname' WHERE `id` = '$id'");
$URL="myprofile.php?id=$id";
header ("Location: $URL");
}else{
mysql_query("UPDATE members SET `displayname` = '$displayname' WHERE `id` = '$id'");
$URL="myprofile.php?id=$id";
header ("Location: $URL");
}

I know most of the if statements arent necessary, but yeh thats the code im using that could be the problem?

mlseim
11-07-2006, 08:18 PM
OK ...

Step back and try a different approach ...

Duplicate all of your suspect pages and then begin eliminating
sections of the page until the error goes away. By eliminating,
I mean remove some "includes", and/or sections of <?php ?>

See if you can make most of the page render with the error and
see what part(s) you removed that eliminated the error.

Spookster
11-07-2006, 08:28 PM
Hello there.

No...this has nothing to do with session...though your session_start should always be at the top of your file.

This error is usually generated when you have already used a header("Location: nextpage.php" ); function or any function that modifies the response header of a given request.

to overcome this problem....create a .htaccess file and have the following lines written at the top of the file.

PHP_FLAG output_buffering On
php_value memory_limit "32M"

If you cannot use .htaccess file...google search how you can set the above values using init_set() function...

Good luck
:thumbsup:

That is incorrect.

Spookster
11-07-2006, 08:30 PM
Ok I think I know what the problem is. You are including top.php in another file aren't you? As I said before you cannot send anything to the browser before session_start(). What is in myprofile.php and what is being sent to the browser before top.php is included?

littlejones
11-07-2006, 08:53 PM
I think Spookster is right, are you including top.php somewhere within myprofile.php? If you are then you must make sure you haven't sent any header("location: url.php") or any other session_start() declarations.

tribalmaniac
11-07-2006, 09:25 PM
thanks for all your help guys, i managed to fix it by swapping all the else if statements for if statements
im guessign it was because it was going inside each of the if statements and reading the header then going ontot he next one and reading the next header etc etc?

Spookster
11-07-2006, 09:36 PM
So the problem wasn't because you were including top.php in myprofile.php and were outputting something to the browser prior to including top.php? It's hard to help you when you don't post all of the relevant code. I would venture so say that by moving some if/else statements around you inadvertently caused the problem to disappear but not necessarily fixed the problem the correct way. Post what you have in myprofile.php and what you have in top.php. If they are large files then just attach the files to the post. I'm not sure that you understand what we are talking about when we say that the session_start function must be executed prior to outputting anything to the web browser. Output can consist of headers which you don't see, any HTML code or any whitespace.

ess
11-10-2006, 06:43 PM
That is incorrect.

Well...I have been using the above mentioned flags on my server for over year and has been working without any problems.

However, recently it came to my attention that some hosting companies has disallowed setting output_buffering on using .htacess files.

Therefore, if you do not have access to your php.ini file to modify the setting and set output_buffering On, than you should type ob_start(); at the beginning and type ob_end_flush(); end of your file that generates the error stated above.

For more information, please read PHP Documentation (http://uk2.php.net/manual/en/function.ob-start.php).

thanks
ess

Spookster
11-10-2006, 07:07 PM
Well...I have been using the above mentioned flags on my server for over year and has been working without any problems.

However, recently it came to my attention that some hosting companies has disallowed setting output_buffering on using .htacess files.

Therefore, if you do not have access to your php.ini file to modify the setting and set output_buffering On, than you should type ob_start(); at the beginning and type ob_end_flush(); end of your file that generates the error stated above.

For more information, please read PHP Documentation (http://uk2.php.net/manual/en/function.ob-start.php).

thanks
ess

I am quite familiar with all things PHP. I wasn't saying that was incorrect. I was saying your statement that said that the error has nothing to do with sessions was incorrect. That error can and definitely will produce that type of error if you have already outputted something to the browser.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum