My head hurts from hitting this brick wall.

Permalink
I have the following code in my package installer controller. $_SESSION['NewPage'] updates just fine with every page load. $_SESSION['PrevPage'] doesn't and stays unset. I have tried everything, including renaming the session variable. What am I missing?

This is happening in C5 version 5.6.2.1

I have the following code in my package installer controller:
public function on_start() {
  Events::extend('on_before_render', 'MessageBarPackage', 
                 'new_page_load', DIR_PACKAGES . 
                 '/message_bar/controller.php');
}
public function new_page_load() {
  $page = Page::getCurrentPage();
  if ($_SESSION['NewPage'] != $page->getCollectionHandle()) {
    $_SESSION['PrevPage'] = 'PreviousPage';
    $_SESSION['NewPage'] = $page->getCollectionHandle();   
  }
  echo 'New['.$_SESSION['NewPage'].'] 
        PrevPage['.$_SESSION['PrevPage'].']';
  if (!isset($_SESSION['PrevPage'])) echo '<br />PrevPage is unset!';
}

ThomasJ
 
exchangecore replied on at Permalink Reply
exchangecore
That's definitely odd. Just as a quick test I threw your code just below the Events::fire('on_page_view', $c, $u); in the dispatcher.php file just to get a quick test rolling and everything worked as expected.

Just to rule out possibilities turn off the cache maybe? (though I wouldn't expect this to affect anything here). Also, you should note that during a "logout" the session is completely destroyed so there's always that.

FWIW I did my quick dispatcher.php test on 5.6.3.1, I doubt it'd be any different on 5.6.2 though.

Edit: Is it possible you're resetting that session variable accidentally somewhere? Maybe doing a set operation instead of a compare operation later on in code?
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
The labels you are using are very generic. $_SESSION is global, so perhaps the labels you are using are conflicting with other $_SESSION uses such as the dashboard page breadcrumb.

From a PRB point of view, as a minimum you need to namespace any use of $_SESSION, either with a prefix or as an array item.

$_SESSION['my_namespace']['NewPage'] = ....;
$_SESSION['my_namespace']['PrevPage'] = ....;
ThomasJ replied on at Permalink Reply
ThomasJ
I am working with all caching turned off. I have already tried changing the variable name twice. The code I show is in the installer in the method that runs on the Events trigger. It is simply setting the variables and then the next lines, printing them. As far as I know, this can't be happening. I think my next step is to re-install c5. And, I'll namespace my session variables.

Thanks.
ThomasJ replied on at Permalink Reply
ThomasJ
I just added namespacing as suggested making it an array and it works fine now. I guess this goes to show that namespacing is very important. I changed the variable name twice and all three names must have been bad picks.

With this experience, I will be thinking about target naming all my work.