Session Error

8 posts by 3 authors in: Forums > CMS Builder
Last Post: Tuesday at 3:35pm   (RSS)

After moving to a new hosting service, I have been receiving the error below when access pages that are secured using the SiteLok.  I can load pages that are not secured.  If I clear the cookies and site data for the website, the pages load fine and I can log out and in many times with no issue.

Error: Session already started, only call session_start() after loading CMS libraries so correct session handler is used. in /var/www/vhosts/sctfpa.gov/httpdocs/sctf/index.php on line 10

I'm trying to track down if this is a CMS Builder, SiteLok or server issue.  If anyone could shed some light on this or point me to a possible cause or reason for the error, that would be great.

Thanks

Phil

Hello Phil,

In this case the error is pretty explicit. Somewhere there is a session_start() call being made prior to the CMSB view_functions.php being included. In index.php, do you see session_start() at the top and before the viewer_function.php include code? If it is, try either deleting the session_start() (as that is being started in the viewer_functions.php include code) or moving it down after the viewer_functions.php code is being included.

If you don't see that happening, perhaps SiteLok is starting that session and it is conflicting with the viewer_functions.php session handling. One way to find this out is if the index.php page (which is generating the error) suddenly works after you disable SiteLok and then doesn't work again when you enable SiteLok that can give you the vital clue that it is SiteLok that is the issue.

Let us know how it goes! 

Tim Hurd
Senior Web Programmer
Interactivetools.com

Tim,

SiteLok definitely starts its own session.  I tried to move the code that calls SiteLok below the CMSB code which did help a little, but I still get the session error every now and then.  Below is the code that is at the beginning of most pages on the site.  Of course it varies depending on the content for that page.

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = ['','../','../../','../../../','../../../../']; // add if needed: '/var/www/vhosts/sctfpa.gov/httpdocs/'
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
  
  // load records for News and Events
  list($news_and_eventsRecords, $news_and_eventsMetaData) = getRecords(array(
    'tableName'   => 'news_and_events',
    'loadUploads' => '1',
    'allowSearch' => '0',
  ));
?>
<?php
$groupswithaccess="TFUSER,HC";
require_once("../slpw/sitelokpw.php");
?>

You will see the last 4 lines are what calls Sitelok.  Here it is below the CMSB code.  It if is before, I get a lot more of the session errors.  I am also working with the developer of SiteLok on this.  They just call a normal session which I'm sure CMSB is doing.

I have run this site for years without this issue and no changes until we moved to the new server.  We are on PHP version 8.3.16.  I may try using 8.2.27 and see if that makes any changes.  The old server it was on was running version 8.1

Hi Phil, 

Sessions are shared by all PHP libraries that are used.  Are you able to comment out the SiteLok code that starts a session?  Or is it possible the remaining errors might be from pages where the CMSB library code isn't up at the top?

We need to either comment out any SiteLok session_start() code or ensure the CMSB session_start() code (in the included libraries) is called first so sitelok can detect it.

Dave Edis - Senior Developer
interactivetools.com

Hi Phil, 

We started storing sessions in MySQL in version 3.67 (released April 12, 2024), which might be related.

There can also be differences in the output buffering settings in php.ini between different servers.

On the page where you're still getting errors, what is the error? And can you ensure you're loading SiteLok before outputting and HTML?

Dave Edis - Senior Developer
interactivetools.com

Dave,

I have tried loading SiteLok before and after the CMSB libraries.  The error I am getting is below.

Error: Session already started, only call session_start() after loading CMS libraries so correct session handler is used. in /var/www/vhosts/sctfpa.gov/httpdocs/sctf/contentDetail.php on line 9

I have several pages that are protected by SiteLok with the code snippet in the above example.  If I load it after CMSB, I can get in without the error until I try to load another page that is protected with SiteLok.  Then I get the error above.

It must be something with the server because I am running this exact same setup on other sites hosted at a different host with no problems.  I can check on the PHP settings.  What should the output buffering settings be?

Thanks

Phil

Hi Phil, 

You want to load the CMSB library code first so you can use our sessions.  

If you want to fill out a second level support request here with steps to reproduce I can take a look for you and make sure there's nothing in CMSB that's preventing it from working:
https://interactivetools.com/support/request/

Thanks!

Dave Edis - Senior Developer
interactivetools.com