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 Dave,

I looked through the SiteLok code there are several places it has a session_start().  I could try to comment out all of them, but that may have an impact on how it functions.

What I find odd is this setup worked for quite a long time until moving to the new server.  We are in the process of rebuilding the entire site and I won't be using some of the unique code that I use now in CMSB which may help. 

What I do in CMSB is with one .php file named contentDetail.php I set the table and other items in the CMSB code so I would not need to have a lot of different actual pages.  See the code below.  Things are set by the menu items selected on the page.  I have found that if I move SiteLok code below the CMSB code on other pages, it works ok, but on this page, if the code is below the CMSB code, I still get session errors.

I will see what the SiteLok developer come back with.  Obviously there is something different in how this server handles sessions that how the old server did.

<?php
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = ['','../','../../','../../../','../../../../','/var/www/vhosts/sctfpa.gov/httpdocs/']; // 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 the page called. 
  list($myRecords, $myMetaData) = getRecords(array(  
    'tableName'   	  => $_GET['section'],
    // 'tableName'   	  => $_REQUEST['section'],  
    'where'           => "num = '".intval(@$_GET['num'])."'", 
    'loadUploads'     => false, 
    'allowSearch'     => false, 
    'limit'           => '1',  
  ));  
  $myRecord = @$myRecords[0]; // get first record 
   
  //get uploads for the selected record meeting minutes
  list($upload_minutesRecords, $upload_minutesMetaData)=getRecords(array( 
    'tableName'       =>      'uploads', 
    'allowSearch'     =>      false, 
    'where'			  =>	  "tableName='".mysql_escape(@$_GET['section'])."' AND fieldName='meeting_minutes' AND recordNum='".intval($myRecord['num'])."'",
    'orderBy'   	  =>      "STR_TO_DATE(info1,'%M %d,%Y') DESC",
    ));
  
  //get uploads for the selected record other docs
  list($upload_otherRecords, $upload_otherMetaData)=getRecords(array( 
    'tableName'       =>      'uploads', 
    'allowSearch'     =>      false, 
    'where'			  =>	  "tableName='".mysql_escape(@$_GET['section'])."' AND fieldName='other_docs' AND recordNum='".intval($myRecord['num'])."'",
	'orderBy'		  =>	  "`order`",
    ));	
?>

Later on the page is where I loop through all of the menu items that grab the info that is then used by the CMSB code to know what content to display.

		<?php 
		  $categoryTablenames = array('exec_comm', 'ag_sc', 'bii_sc', 'com_it_sc', 'cj_sc', 'exercise_wg', 'frhm_sc', 'health_and_safety', 'hlth_med', 'imt', 'info_intel_wg', 'links', 'logs_eqp', 'mass_evac_wg', 'pol_procd', 'pio', 'tng_sc', 'exercise_wg');
		 
		  $categoryRecordSets = array(); 
		 
		  foreach ($categoryTablenames as $tablename) { 
			list($categoryRecords,) = getCategories(array( 
			  'tableName'   => $tablename, 
			)); 
			$categoryRecordSets[$tablename] = $categoryRecords; 
		  } 
		?> 
		 <ul id="theMenu">
		 <li><a href="index.php">Home</a></li>
		  <?php foreach($categoryRecordSets as $tablename => $categoryRecords): ?>
			  <?php foreach ($categoryRecords as $record): ?>
				<?php echo $record['_listItemStart'] ?>
					<?php if (sl_ismemberofany($record['visible_grp'])){ ?>
					<a href="contentDetail.php?section=<?php echo $tablename ?>&amp;num=<?php echo $record['num'] ?>"><?php echo htmlspecialchars($record['name']) ?></a>
					<?php } ?>
				<?php echo $record['_listItemEnd'] ?>
			 <?php endforeach ?>
		  <?php endforeach ?>

So I can get things working with the index.php page, but if I mess around with the contentDetail.php, which has the code above, it messes things up a bit too much.

Hopefully I won't have these issues with the new site.  I will be using SiteLok and CMSB on it in much the same way.  

Thanks

Phil

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