Fatal errors if spaces or blank lines exist between PHP tags using Website Membership plugin

8 posts by 3 authors in: Forums > CMS Builder
Last Post: March 26, 2019   (RSS)

By Steve99 - March 25, 2019

Hi Jerry,

I'm not seeing any spaces or blank lines before the opening tag on CMSB code generated for website membership pages. Example:

<?php
  
  // code here

?><!DOCTYPE html>

A space or blank line before the opening tag outputs to the browser, and as soon as there is any output to the browser you can no longer set or modify header info.

This code line checks for and sets the "lastUrl" cookie which will only work if there is no previous output to the browser:

if (!getPrefixedCookie('lastUrl')) { setPrefixedCookie('lastUrl', @$_SERVER['HTTP_REFERER'] ); }

From your referenced code block, change this:

<?php
// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home3/mrqsygmy/public_html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
if (!@$GLOBALS['WEBSITE_MEMBERSHIP_PLUGIN']) { die("You must activate the Website Membership plugin before you can access this page."); } ?> 

<?php // error checking
$errorsAndAlerts = alert();
if (@$CURRENT_USER) { $errorsAndAlerts .= "You are already logged in! <a href='/'>Click here to continue</a> or <a href='?action=logoff'>Logoff</a>.<br/>\n"; }
if (!$CURRENT_USER && @$_REQUEST['loginRequired']) { $errorsAndAlerts .= "Please login to continue.<br/>\n"; }

// save url of referring page so we can redirect user there after login
if (!getPrefixedCookie('lastUrl')) { setPrefixedCookie('lastUrl', @$_SERVER['HTTP_REFERER'] ); }

?>
<!DOCTYPE html>

To this:

<?php
// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home3/mrqsygmy/public_html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
if (!@$GLOBALS['WEBSITE_MEMBERSHIP_PLUGIN']) { die("You must activate the Website Membership plugin before you can access this page."); } 

// error checking
$errorsAndAlerts = alert();
if (@$CURRENT_USER) { $errorsAndAlerts .= "You are already logged in! <a href='/'>Click here to continue</a> or <a href='?action=logoff'>Logoff</a>.<br/>\n"; }
if (!$CURRENT_USER && @$_REQUEST['loginRequired']) { $errorsAndAlerts .= "Please login to continue.<br/>\n"; }

// save url of referring page so we can redirect user there after login
if (!getPrefixedCookie('lastUrl')) { setPrefixedCookie('lastUrl', @$_SERVER['HTTP_REFERER'] ); }

?><!DOCTYPE html>

Then you should no longer experience that error.

Let me know how you make out.

Best,
Steve

By gkornbluth - March 25, 2019

Hi Steve,

Sorry I took so long to respond, and, thanks for all your effort.

I'll take a look first thing tomorrow morning and let you know what I get.

Best,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By gkornbluth - March 26, 2019

Good morning Steve,

You're right, and I've been using a similar approach to work around the problem, but I don't understand why this has become an issue with PHP 7.2

I've used the same files with PHP5.6 and didn't have this problem.

I makes me concerned that there will be a lot of clients with a lot of files exhibiting this issue moving forward.

Especially since I use a number of includes for the load viewer library and load record calls.

Any ideas what's going on?

Thanks,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Steve99 - March 26, 2019

Good Morning Jerry,

I suppose it's possible the "lastUrl" cookie was already set so the code didn't attempt to do so. If you're noticing other session issues on that server it could have something to do with the session path. In general, the solution is to not have any output before functions that send or modify HTTP headers.

I came across a forum post with a link from iTools crew to a very thorough StackOverflow post describing this:

Forum Link
https://www.interactivetools.com/forum/forum-posts.php?postNum=2239437#post2239437

StackOverflow Link
https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php/8028987#8028987

Best,
Steve

By gkornbluth - March 26, 2019

Thanks Steve,

I'll dive in deeper and see what I can come up with

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Dave - March 26, 2019

Hey Guys, 

The reason why you'll get "headers already sent" whitespace errors on some installations and not others is because of the PHP "output_buffering" setting.  

When it's set to zero ("0") output is sent right away and the moment you send page content you can no longer send HTTP headers (cookies, etc).

When it's set to anything else PHP buffers the output before sending it and lets you still send HTTP headers even if some page content has already been sent.  Even though they're being sent out-of-order so to speak, PHP sends them in the proper order to the browser.

If your code was relying on output buffering you can enable it on a new host in an .htaccess, php.ini, or .user.ini file.  Setting it to 4096 for a 4k buffer should resolve most of those issues.  

References: 

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

By gkornbluth - March 26, 2019

Wow Dave...

Thanks for that great explanation.

Didn't have any idea about the output buffering thing.

I'll give it a try.

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php