Headers already sent errrors if not logged in.
8 posts by 2 authors in: Forums > CMS Builder
Last Post: September 19, 2022 (RSS)
I’m getting a lot of ‘header already sent’ errors on a site that I haven’t looked at for a while and the problem seems to affect a number of pages which I know all worked before.
In all cases the pages contain the code :
if (!$CURRENT_USER) { websiteLogin_redirectToLogin(); } and are restricted to logged in members only (and sometimes only admins).
All the pages work when I’m logged in, however if I’m logged off, they show errors like: “Fatal error: setPrefixedCookie: Can't set cookie(lastUrl, https://dbtproviders.com/updaterequiredreport.php), headers already sent! Output started in /home3/mrqsygmy/public_html/_website_init.php line 77. in /home3/mrqsygmy/public_html/cmsAdmin/lib/http_functions.php on line 292"
I’m running CMS Version 3.55 and PHP version 7.4.28 and website membership 1.13 on the site (PHP version 8.1 throws too many errors about deprecate commands, etc., but I’m reticent to upgrade CMSB before solving the current issue)
I’ve already tried my usual approach of copying the viewer library code from a working page into a new document and pasting the remaining code from the broken page to the new one, but that doesn’t seem to help.
I’ve tried all the tricks I can think of, and all that I’ve found in the forum and in my cookbook, and I sure could use some guidance.
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
By daniel - September 19, 2022
Hi Jerry,
What is the code at line 77 of /home3/mrqsygmy/public_html/_website_init.php line 77? This is where the error indicates the early output is happening.
Technical Lead
interactivetools.com
By gkornbluth - September 19, 2022 - edited: September 20, 2022
Hey Daniel,
Again thanks for checking on this.
When I delete everything below the load records calls in _website_init, (starting at line 77) the error message points to the 'include' line in the provider profile file (line 7).
I've already tried inserting the load viewer function and load records directly into some of the files that exhibit the issue and the error still remains only when logged off.
You can see the issue at https://dbtproviders.com/provider_profile.php, and at https://dbtproviders.com/providers_emaillist.php, as examples
Hope I'm making sense.
Thanks,
Jerry
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
By daniel - September 19, 2022
Hey Jerry,
I can see that there's a space after the "?> " on line 7 in provider_profile.php - whitespace will count as "output" and cause this error. To make things as simple as possible, I'd suggest moving the login check to be immediately after the include, like this:
include ("_website_init.php");
if (!$CURRENT_USER) { websiteLogin_redirectToLogin(); }
?>
Also, PHP tags that look like this could be outputting a newline which I assume is also being counted as output:
?>
<?php
This shows up a number of times in _website_init.php starting on line 76-77, so I would suggest rewriting the whole file to exist in a single PHP block like this:
//...
list($faqRecords, $faqMetaData) = getRecords(array(
'tableName' => 'faq',
'loadUploads' => true,
'allowSearch' => false,
));
$masterurl = $common_informationRecord['master_url'];
if ($image_libraryRecord['image_1']):
foreach ($image_libraryRecord['image_1'] as $index => $upload):
$image1 = $upload['thumbUrlPath2'];
endforeach;
endif;
// etc, etc.
or simply remove the newlines so there's no space between PHP tags, like this: "?><?php"
Let me know if that helps, or if you have any further questions.
Thanks!
Technical Lead
interactivetools.com
Thanks Daniel, I really appreciate a second set of (expert) eyes.
I'll let you know the outcome
I'm surprised though that it only kicked in when logged out.
Jerry
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
By gkornbluth - September 19, 2022 - edited: September 19, 2022
Well, I'm embarrassed, and a bit confused.
I rewrote the _website_init so that it was 1 block of php code, changed the placement of the if (!$CURRENT_USER) { websiteLogin_redirectToLogin(); } and everything seems to work.
The confusing part is that I use the same _website_init file on many of my sites and never seemed to have the same issue.
Oh well, better go back and check all the other sites again...
I've not used the shorthand code below before, and just to be on the safe side, could you tell me if I've formatted it correctly?
Thanks,
Jerry
list($faqRecords, $faqMetaData) = getRecords(array(
'tableName' => 'faq',
'loadUploads' => true,
'allowSearch' => false,
));
$masterurl = $common_informationRecord['master_url'];
if ($image_libraryRecord['image_1']){
foreach ($image_libraryRecord['image_1'] as $index => $upload):
$image1 = $upload['thumbUrlPath2'];
endforeach;}
if ($image_libraryRecord['image_2']){
foreach ($image_libraryRecord['image_2'] as $index => $upload):
$image2 = $upload['thumbUrlPath2'];
endforeach;}
if ($image_libraryRecord['image_3']){
foreach ($image_libraryRecord['image_3'] as $index => $upload):
$image3 = $upload['thumbUrlPath2'];
endforeach;}
if ($image_libraryRecord['image_4']){
foreach ($image_libraryRecord['image_4'] as $index => $upload):
$image4 = $upload['thumbUrlPath2'];
endforeach;}
if ($image_libraryRecord['image_5']){
foreach ($image_libraryRecord['image_5'] as $index => $upload):
$image5 = $upload['thumbUrlPath2'];
endforeach;}
if ($image_libraryRecord['image_6']){foreach ($image_libraryRecord['image_6'] as $index => $upload):
$image6 = $upload['thumbUrlPath2'];
endforeach;}
if ($image_libraryRecord['image_7']){
foreach ($image_libraryRecord['image_7'] as $index => $upload):
$image7 = $upload['thumbUrlPath2'] ?><?php endforeach;}
if ($image_libraryRecord['image_8']){foreach ($image_libraryRecord['image_8'] as $index => $upload):
$image8 = $upload['thumbUrlPath2'];
endforeach;}
if ($image_libraryRecord['image_9']){ foreach ($image_libraryRecord['image_9'] as $index => $upload):
$image9 = $upload['thumbUrlPath2'];
endforeach;}
if ($image_libraryRecord['image_10']){foreach ($image_libraryRecord['image_10'] as $index => $upload):
$image10 = $upload['thumbUrlPath2'];
endforeach;}
if ($image_libraryRecord['image_11']){ foreach ($image_libraryRecord['image_11'] as $index => $upload):
$image11 = $upload['thumbUrlPath2'];
endforeach;}
if ($image_libraryRecord['image_12']){
foreach ($image_libraryRecord['image_12'] as $index => $upload):
$image12 = $upload['thumbUrlPath2'];
endforeach;}
$placeHolders = array("*img1*", "*img2*", "*img3*", "*img4*", "*img5*", "*img6*", "*img7*", "*img8*", "*img9*", "*img10*", "*img11*", "*img12*");
$replaceWith = array(@$image1, @$image2, @$image3, @$image4, @$image5, @$image6, @$image7, @$image8, @$image9, @$image10, @$image11, @$image12 ); ?>
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
By daniel - September 19, 2022
Hey Jerry,
Yes, that formatting looks correct! There is one "?><?php" remaining that you can remove, but it's pretty inconsequential.
The confusing part is that I use the same _website_init file on many of my sites and never seemed to have the same issue.
Oh well, better go back and check all the other sites again...
There is a setting that can help PHP ignore some premature output, but I've found it to not always be 100% reliable, and it can be site/server-dependent, so there may be some difference between this environment and other sites where it's been used. In any case, I find it best practice to just remove any unnecessary whitespace to keep code as portable as possible.
Let me know if you have any further issues!
Thanks,
Technical Lead
interactivetools.com
Hey Daniel,
Got the rogue ?><?php.
Really glad you could help solve the issue.
I'll go ahead and have my crow stew dinner now.
Thanks again!
Jerry
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php