Jump to content

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


psy
 Share

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
    $session->redirect($loginPage->url);
}

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;

    $session->redirect($goToUrl);
} 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.

tia

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

Link to comment
Share on other sites

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

$url = $page->url;

I've read the docs  at https://processwire.com/api/ref/session/ 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

 

Link to comment
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.

Link to comment
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. :) 

Link to comment
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?

2018-01-20_085230.png.80bd8405ba1c2466a924684542fc2cf8.png

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...

/login/?return={id}

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

  • Like 2
  • Thanks 1
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...