[solved] $session incorrectly storing string from $page var

Recommended Posts

I've used this code on another site (same web host) and it all works fine.

When a visitor lands on a page and they're not logged, the page name/path/url/httpUrl (tried them all) is saved to a session var. Code in _init.php is:

$loginPage = pages( 1085);
if(!$user->isLoggedin() && $page->id != $loginPage->id) { // not for login page
    $session->set('returnPage', $page->path); // results in /http404/ stored in session var
//    $session->set('returnPage', '/rants/'); // works fine

Code in the LoginRegister template:

if($user->isLoggedin() && !$input->get('profile') && !$input->get('logout')) {
    // login and go back to the previous page or go to the home page
     $goToUrl = $session->get('returnPage') ? $session->get('returnPage') : '/';

    var_dump($session->getAll()); die;

} else {
    // let the LoginRegister module have control
    $content = $modules->get('LoginRegister')->execute();

This var_dump shows that the returnPage session variable is stored as the path to the 404 error page

["returnPage"]=> string(9) "/http404/"

I also tried $page->id with the resulting var (int) 27 which is the 404 Page id.

Also tried namespace in the session var...

It all worked fine when I manually typed in a valid page path, ie only weirdness when I used the $page var.

Any help to explain why this is happening and how to fix greatly appreciated.


Share this post

Link to post
Share on other sites
8 hours ago, psy said:

$loginPage = pages( 1085);

Have you tried

$loginPage = $pages->get(1085);


Share this post

Link to post
Share on other sites

@BitPoet thanks for the suggestion. I have 

$config->useFunctionsAPI = true;

in my config.php and even after trying your suggestion, it failed.

There is no problem with the $session redirecting to the login page with either coding version. The problem appears to be that somewhere, $session tries & fails to evaluate $page->httpUrl, or $page->id, or $page->path, or $page->anything

If it helps, I'm using:

PW version: 3.0.88

PHP version: 7.0

Share this post

Link to post
Share on other sites

Ah, sorry, I misread the code there a bit. $session doesn't evaluate anything in your code, so I'd take a look and see if $page really contains what you expect it to in _init.php.

Share this post

Link to post
Share on other sites

@BitPoet, yep tried that (proven with var_dump($page->url); die ... in contains the current page url or id or whatever... even tried:

$url = $page->url;

I've read the docs  at and looked at core/Session.php - I can't see anything either. It's got me stumped!

What's weirdest is if I type in the page path, eg '/rants/' - it all works


Share this post

Link to post
Share on other sites

Does putting $page->path in double quotes change anything? Does the network tab show any requests going on with a 404 return code when all other browser tabs are closed?

Share this post

Link to post
Share on other sites

@BitPoet tried the double quotes... tried everything I could think of including trying with only one tab open in FF and definitely logged out. Use Chrome for dev.

Share this post

Link to post
Share on other sites

That's really, really curious. Is there any other code in site/(ready|init).php or _init.php that might be the culprit? Perhaps even stale code, so deleting site/assets/cache/FileCompiler and testing again might make sense.

Share this post

Link to post
Share on other sites

@BitPoet yes, really curious. Nothing in init.php and very little, let alone anything I would suspect would cause an issue in ready.php.

I made an autoloaded module of custom functions rather than _func.php and will check it thoroughly, but again doubt it. It mostly comprises mark-up output.

Will try your suggestions and report back - but not right now, it's 10pm on Friday night here and just a bit over it. Thanks for your ideas. :) 

Share this post

Link to post
Share on other sites

@psy You're welcome. I hope you find something. Sometimes, a little break is exactly what one needs to spot an issue. Have a nice evening. :)

  • Like 1

Share this post

Link to post
Share on other sites

Most likely explanation is that the page path or ID that you are saving to session is correct, and that it simply is the 404 page that is being loaded. Maybe you have removed view access for the guest role on one or more templates, and have the "Show a 404 page" option selected?


Incidentally, an alternative to what you are doing in _init.php is to select the option "Redirect to another URL" to redirect to your login page, and pass the ID of the page that the user attempted to access in a GET variable. So something like...


...then you look for the return variable in your login template and redirect back to that page after login.

  • Like 2
  • Thanks 1

Share this post

Link to post
Share on other sites

@Robin S You nailed it! Thank you.

Don't recall changing any template permissions but must have done at some stage. They were certainly all set to the defaults. No matter, problem solved.

Cheers, psy

Share this post

Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By eutervogel
      On my website the user is able to choose between to styles of a gallery. A gridview and a stripe view.
      I want to store the choice during the whole session. 
      So if the user chooses a style I do it like so:
      $query = $_GET['view'];     if($query){         $session->set('view', $query);     } So that's working pretty fine, but after a random number of menuclicks or reloads its gone. Sometimes it is stored for 10 -15 pageloads and sometimes it's gone after 2 loads,
      Lifetime is set to 3600 in php.ini
      session.gc_maxlifetime = 3600  
      This is how I look if it exists:   
       if($session->view == 'grid'){         include('album-grid-title.php');         $session->set('view', $query);     }     elseif($session->view == 'stripes'){         include('album-stripes.php');         $session->set('view', $query);     }     else{         include('album-stripes.php');            }  
      Can anyone point me in the right direction and tell me what I'm doing wrong or why $session gets lost?
      Thanks in advance
    • By benbyf
      Is there a way to restrict logins for users so that one user can't be loggedin in two places at the same time?
      e.g. auto logout user after inactivity (of say 15 minutes..?), or logout action and disallow login if user still "logged in" somewhere?
    • By Martin Muzatko
      Hello there!
      I want to create a user front-end (user can register/login/logout via templates)
      I'm working based on the intermediate site profile. So _init.php is loaded first, then the template file and then _main.php.
      I integrated the custom login as described here, and changed it to my needs. ( I don't want to redirect the user, if the form is filled in successfully)
      The problem I face, is that $user->isLoggedin() lags behind $session->login().
      Which means that whenever I do a login, I DO get the information that the login was successful ($session->login(...) instanceof User). I COULD use that information on _main.php to show a profile in the upper right corner.
      However I don't want to set a variable in the template and ask for it in _main.php. Are there any alternatives? Is a redirect really required to complete the session handling? Why? I have the same problem for the logout. The user is still displayed as logged in, when he opens the logout page.
      Thanks in advance.
    • By Mirza
      How to track user active time based on session login and logout.
      Basically, I want to get the report that each user login time and logout time/session inactivity time.
      Is there any module available or we can use any hooks to simulate the above.
      Thanks in advance for your support.
    • By suntrop
      Hi all. Perhaps this is just my fault, but I can't get behind it :-(
      When I store an array in a $session and want to read its contents – after a $session->redirect() – it is NULL by var_dump()
      If I use the exact same code and just store a string or a Page ID the output is correct.
      // file-1, product page $last_add_to_cart['product'] = $selected_product; $last_add_to_cart['quantity'] = $selected_product_quantity; $session->last_add_to_cart = $last_add_to_cart; $session->redirect('/warenkorb/'); // file-2, cart var_dump($session->last_add_to_cart); // is NULL Strangely this works 
      // file-1, product page $last_add_to_cart['product'] = $selected_product->id; #$last_add_to_cart['quantity'] = $selected_product_quantity; $session->last_add_to_cart = $last_add_to_cart; $session->redirect('/warenkorb/'); // file-2, cart var_dump($session->last_add_to_cart); // is 1234 EDIT: When I var_dump the $session before the redirect everything is ok