Membership Plugin PHP8

4 posts by 3 authors in: Forums > CMS Builder
Last Post: May 24, 2022   (RSS)

By dbown - May 20, 2022

I have some code that I used in the Website Membership Plugin that work in PHP 7.3 but not in 8.0.

When using PHP 8.0, when a user logs out of the website the following message appears...

<START>
Warning: Trying to access array offset on value of type bool in /home1/XXXXX/public_html/client-area.php on line 18 Fatal error: setPrefixedCookie: Can't set cookie(lastUrl, https://www.XXXXX.com/client-area.php), headers already sent! Output started in /home1/XXXXX/public_html/client-area.php line 18. in /home1/XXXXX/public_html/cmsb/lib/http_functions.php on line 292
<END>

The code on the top of the page is as follows:

<START>

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
// load viewer library
$libraryPath = 'cmsb/lib/viewer_functions.php';
$dirsToCheck = array('/home/eeltd3/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='https://www.XXXXXX.com/client-area.php'>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'] ); }

$CompName = $CURRENT_USER['fullname'];

// load records from 'documents'
list($documentsRecords, $documentsMetaData) = getRecords(array(
'tableName' => 'documents',
'loadUploads' => true,
'allowSearch' => false,
'where' => "title = '$CompName'",
));

?>
<?php if (!$CURRENT_USER) { websiteLogin_redirectToLogin(); } ?>

<END>

Hopefully that is enough to go on for you to help me fix this issue.

Thanks,
Dave

By daniel - May 24, 2022

Hi Dave,

The error is being caused by this line:

$CompName = $CURRENT_USER['fullname'];

It is being referenced before the page checks to see if the user is logged in (this is what creates the $CURRENT_USER array). So a simple fix would be to move this line that checks for a logged-in user:

<?php if (!$CURRENT_USER) { websiteLogin_redirectToLogin(); } ?>

And move it above the line at issue:

if (!$CURRENT_USER) { websiteLogin_redirectToLogin(); }
$CompName = $CURRENT_USER['fullname'];

Let me know if that fixes the issue, or if you have any other questions!

Thanks,

Daniel
Technical Lead
interactivetools.com

By dbown - May 24, 2022

Thank you Daniel.

That worked perfectly!

Cheers,
Dave