close ad
WARNING: Do Not Install the DREAMWEAVER CC 2017 Update »
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Troubleshooting the Headers Already Sent PHP Error

Thread began 3/03/2011 11:10 am by Jason Byrnes | Last modified 10/05/2011 7:44 pm by Gooner10 | 13146 views | 7 replies |

Jason ByrnesWebAssist

Troubleshooting the Headers Already Sent PHP Error

The Dreaded Headers Already Sent error message may appear on your page:

  Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /Applications/MAMP/htdocs/supportSite/events_Results.php:6) in /Applications/MAMP/htdocs/supportSite/events_Results.php on line 9  



Put simply, the cause of the error is output on the page before the PHP session_start() or header() functions are used.

But how do you make it go away? the Key to fixing this issue is to look closely at the part in parentheses, this tells you where to look for the output:

  (output started at /Applications/MAMP/htdocs/supportSite/events_Results.php:6)  



this tells me to look at line 6 of the events_Results.php page.


In the first screen shot that I have attached, you can see that there is a closing PHP Tag (?>) at line 4. Line 5 is a blank line and line 6 is a new opening php tag (<?php).

it is the blank line at line 5 that is causing the error to occur.

If you dont see a blank line, it could be a blank space. In the second screen shot, I have enabled Hidden Characters (View -> Code View Options -> Hidden Characters), this shows line breaks and spaces in the code. This shows that line 4 has a closing PHP tag followed by a blank space before the line break. that blank space after the closing PHP Tag:
"?> "

Will cause the headers Already sent error as well.

Not just blank space, but actual text between php code blocks:

php:
?>

hello world
<?php




or HTML comments:

php:
<?

<!-- this is an html comment -->
<?php



can cause the error.


There are 2 other causes of the Headers already sent error besides output to the page. Look for these causes if the output is reported at line 1 in the error:
1) Using the virtual() function to include php files instead of require once:

php:
<?php virtual('Connections/connEAF.php'); ?>



In Dreamweaver's site settings panel, the Local Info tab has a settings for "Links relative To" if you select Site Root (see the third screen shot), Dreamweaver will use the virtual() function for including files. You should set this to Document instead so that Dreamweaver will use the require_once() function.

2) The Unicode Signature (BOM) PHP bug. the forth screen shot shows the Title / Encoding category of the Page Properties screen (Modify - > Page Properties). Checking the Include Signature (BOM) option can cause the headers already sent error

Sign in to reply to this post

James L

headers already sent error

I've checked for spaces at beginning of lines in my index.php file. Checked Dreamweaver items 1) and 2),
I still get this error message:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /Applications/MAMP/htdocs/ProRes_from_Bill/TMP6WT0WISSBX.php:5) in /Applications/MAMP/htdocs/ProRes_from_Bill/WA_eCart/WA_eCart_Definition_PHP.php on line 465

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/htdocs/ProRes_from_Bill/TMP6WT0WISSBX.php:5) in /Applications/MAMP/htdocs/ProRes_from_Bill/WA_eCart/WA_eCart_Definition_PHP.php on line 465

If I go to WA_eCart_Definition_PHP, line 465 looks OK. This what that whole section looks like:

//Session Storage Functions
function Session_SaveCart() {
if (!session_id()) session_start();
$theCartString = "";
for ($n=0; $n<sizeof($this->Items); $n++) {
$countIt = 0;
if ($n!=0) {
$theCartString .= "§";
}
foreach ($this->Items[$n] as $m) {
if ($countIt != 0) $theCartString .= "|WAECART|";
$theCartString .= $m;
$countIt++;
}
}
$_SESSION[$this->CartName."_Items"] = $theCartString;
}
function Session_GetContent() {
if (!session_id()) session_start();
$retArray = array();
if (isset($_SESSION[$this->CartName."_Items"]) && $_SESSION[$this->CartName."_Items"] != "") {
$theCartString = $_SESSION[$this->CartName."_Items"];
$theItemArray = explode("§",$theCartString);
for ($x=0; $x<sizeof($theItemArray); $x++) {
$theValueArray = explode("|WAECART|",$theItemArray[$x]);
$toExec = "new ".$this->CartName. "_ItemDefinition(";
for ($y=0; $y<sizeof($theValueArray); $y++) {
if ($y != 0) $toExec.=", ";
$toExec .= '"'.str_replace("\'","'",addslashes($theValueArray[$y])).'"';
}
eval("\$nextItem =".$toExec.");");
$retArray[sizeof($retArray)] = $nextItem;
}
}
return $retArray;
}

I'll attach a screen capture so you can see the line numbers...

Please help :)

Sign in to reply to this post

Jason ByrnesWebAssist

the problem is not in the WE_eCart_Definition File at line 465, that is only where the session_start() function is. the problem is at line 5 of the index.php file, look at the error, the output started section:

output started at /Applications/MAMP/htdocs/ProRes_from_Bill/TMP6WT0WISSBX.php:5

tells where to look for the cause of the problem

I'm guessing that you are using Dreamweaver's live preview, that is the cause of the strange TMP6WT0WISSBX.php file name, which is really just your index page. look at line 4 and five of the index page.

Sign in to reply to this post

Gooner10

headers already sent error

I have read through this thread and made sure any additional spaces are removed from my code and I'm still getting the same error. The page that processes the form includes the reCatpcha code following the instructions shown on this thread:

showthread.php?t=17481

Here is my form: form.php

The reCaptcha code is included with the UE code on verify.php. The error indicates the problem is caused on line 4 if (!isset($_SESSION))session_start();

<?php require_once("webassist/email/mail_php.php"); ?>
<?php require_once("webassist/email/mailformatting_php.php"); ?>
<?php
if (!isset($_SESSION))session_start();
?>
<?php
require_once('recaptchalib.php');
$privatekey = "6Ldgz8gSAAAAAMemyys9y56jOW-EIYPh4cYsLaw7";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The code does not match. Please try again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
//WA Universal Email object="mail"
set_time_limit(0);
$EmailRef = "waue_form_1";
$BurstSize = 200;
$BurstTime = 1;
$WaitTime = 1;
$GoToPage = "formsent.html";
$RecipArray = array();
$StartBurst = time();
$LoopCount = 0;
$TotalEmails = 0;
$RecipIndex = 0;
// build up recipients array
$CurIndex = sizeof($RecipArray);
$RecipArray[$CurIndex] = array();
$RecipArray[$CurIndex ][] = "email@email.com";
$TotalEmails += sizeof($RecipArray[$CurIndex]);
$RealWait = ($WaitTime<0.25)?0.25:($WaitTime+0.1);
$TimeTracker = Array();
$TotalBursts = floor($TotalEmails/$BurstSize);
$AfterBursts = $TotalEmails % $BurstSize;
$TimeRemaining = ($TotalBursts * $BurstTime) + ($AfterBursts*$RealWait);
if ($TimeRemaining < ($TotalEmails*$RealWait) ) {
$TimeRemaining = $TotalEmails*$RealWait;
}
$_SESSION[$EmailRef."_Total"] = $TotalEmails;
$_SESSION[$EmailRef."_Index"] = 0;
$_SESSION[$EmailRef."_Remaining"] = $TimeRemaining;
while ($RecipIndex < sizeof($RecipArray)) {
$EnteredValue = is_string($RecipArray[$RecipIndex][0]);
$CurIndex = 0;
while (($EnteredValue && $CurIndex < sizeof($RecipArray[$RecipIndex])) || (!$EnteredValue && $RecipArray[$RecipIndex][0])) {
$starttime = microtime_float();
if ($EnteredValue) {
$RecipientEmail = $RecipArray[$RecipIndex][$CurIndex];
} else {
$RecipientEmail = $RecipArray[$RecipIndex][0][$RecipArray[$RecipIndex][2]];
}
$EmailsRemaining = ($TotalEmails- $LoopCount);
$BurstsRemaining = ceil(($EmailsRemaining-$AfterBursts)/$BurstSize);
$IntoBurst = ($EmailsRemaining-$AfterBursts) % $BurstSize;
if ($AfterBursts<$EmailsRemaining) $IntoBurst = 0;
$TimeRemaining = ($BurstsRemaining * $BurstTime * 60) + ((($AfterBursts<$EmailsRemaining)?$AfterBursts:$EmailsRemaining)*$RealWait) - (($AfterBursts>$EmailsRemaining)?0:($IntoBurst*$RealWait));
if ($TimeRemaining < ($EmailsRemaining*$RealWait) ) {
$TimeRemaining = $EmailsRemaining*$RealWait;
}
$CurIndex ++;
$LoopCount ++;
session_commit();
session_start();
$_SESSION[$EmailRef."_Index"] = $LoopCount;
$_SESSION[$EmailRef."_Remaining"] = round($TimeRemaining);
session_commit();
wa_sleep($WaitTime);
include("webassist/email/waue_form_1.php");
$endtime = microtime_float();
$TimeTracker[] =$endtime - $starttime;
$RealWait = array_sum($TimeTracker)/sizeof($TimeTracker);
if ($LoopCount % $BurstSize == 0 && $CurIndex < sizeof($RecipArray[$RecipIndex])) {
$TimePassed = (time() - $StartBurst);
if ($TimePassed < ($BurstTime*60)) {
$WaitBurst = ($BurstTime*60) -$TimePassed;
wa_sleep($WaitBurst);
}
else {
$TimeRemaining = ($TotalEmails- $LoopCount)*$RealWait;
}
$StartBurst = time();
}
if (!$EnteredValue) {
$RecipArray[$RecipIndex][0] = mysql_fetch_assoc($RecipArray[$RecipIndex][1]);
}
}
$RecipIndex ++;
}
$_SESSION[$EmailRef."_Total"] = 0;
$_SESSION[$EmailRef."_Index"] = 0;
$_SESSION[$EmailRef."_Remaining"] = 0;
session_commit();
session_start();
if ($GoToPage!="") {
header("Location: ".$GoToPage);
}
}

?>

Sign in to reply to this post

Jason ByrnesWebAssist

instead of pasting the page code here, compress the verify.php file into a zip archive and attach the zip file.

Sign in to reply to this post

Gooner10

headers already sent error

Here's the attachment.

Attached Files
verify.zip
Sign in to reply to this post

Jason ByrnesWebAssist

you have blank spaces at the end of lines 1, 2 and 5 which are casing the error.

line 1:
"<?php require_once("webassist/email/mail_php.php"); ?> "

line 2:
"<?php require_once("webassist/email/mailformatting_php.php"); ?> "

line 5:
"?> "


this is the same issue I discuss in the second screen shot of my origianl post:

  If you dont see a blank line, it could be a blank space. In the second screen shot, I have enabled Hidden Characters (View -> Code View Options -> Hidden Characters), this shows line breaks and spaces in the code. This shows that line 4 has a closing PHP tag followed by a blank space before the line break. that blank space after the closing PHP Tag:
"?> "  
Sign in to reply to this post

Gooner10

Thank you. Didn't think to check the other lines as it was only indicating a problem with that one line. Appreciate your help!

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...