Jump to content

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


Recommended Posts

Posted

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

Posted

@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

Posted

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.

Posted

@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

 

Posted

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?

Posted

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

Posted

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.

Posted

@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. :) 

Posted

@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
Posted

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
Posted

@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

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
×
×
  • Create New...