Jump to content

$session variable problem


MarcinP
 Share

Recommended Posts

Hi guys.

I have very strange problem that i cant managed fo few hours. This is realy hard to understand for me.

I work on website and i want it to have banners in few places. In admin i created structure: Banners > Banner Group > Banner 1, Banner2, Banner 3 etc. I want this banners to be loaded one by one rather than randomly. I created function that print baner and save its id to session variable like this:

function renderBannerBox($banner_group = false) { //takes banner group name as argument
	$out = '';			
	if ($banner_group) {
		$bgroup_url = '/banners/' . $banner_group . '/';
		$bgroup = wire(pages)->get($bgroup_url);
		
		if ($bgroup->id) {		
			$bgroup_id = $bgroup->id;
			
			//for every banner group create separate session variable to store displayed banners
			$session_name =  $bgroup->name . '_array'; 
			
			//get session variable with array of ids of displayed banners
			$displayed_banners = wire(session)->$session_name; 
			
			$selector = "id!=";
			$selector .= substr($displayed_banners, 0, -1);
			$selector .= ", template=banner, parent=$bgroup_id";
			
			$banners = wire(pages)->find("template=banner, parent=$bgroup_id");
			
			$banner = $banners->get($selector);
	
			//if no banner (first view or all banners from group displayed) get random banner and
			if (! $banner->id) { 
				$displayed_banners = ''; // clear $displayed banners variable
				$banner = $banners->findRandom(1)->first; //get random banner			
			}
	
			//add current banner id to variable
			$displayed_banners .= $banner->id . '|';
			
			// save variable to $session
			wire(session)->$session_name = $displayed_banners;	
			
			
			// code for html formating	
		}
}	
	
Strange thing is that for every page refresh different number of banners ids are saved to my $session variable. One time is 1 id, second time 2 ids and so on.

You can see it here: http://demo.webego.pl/ginfo/ - above every banner i print current value of $session variable.

I tested it also in my main template file with independent test variable - it works the same way. It looks like page is sometimes 'double loaded'.

Please let me know if you have any idea what is going on.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...