PDA

View Full Version : Troubleshooting the Headers Already Sent PHP Error


Jason Byrnes
03-03-2011, 08:10 AM
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:

?>
hello world
<?php



or HTML comments:
<?
<!-- 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 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

reddingj243756
03-28-2011, 07:47 PM
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 :)

Jason Byrnes
03-29-2011, 07:03 AM
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.

Gooner10
10-05-2011, 07:00 AM
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:

http://www.webassist.com/forums/showthread.php?t=17481

Here is my form: http://mickelectric.mobi/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);
}
}

?>

Jason Byrnes
10-05-2011, 07:06 AM
instead of pasting the page code here, compress the verify.php file into a zip archive and attach the zip file.

Gooner10
10-05-2011, 08:41 AM
Here's the attachment.

Jason Byrnes
10-05-2011, 08:58 AM
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:
"?> "

Gooner10
10-05-2011, 04:44 PM
Thank you. Didn't think to check the other lines as it was only indicating a problem with that one line. Appreciate your help!