Jump to content
psy

[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
    $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

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);

here?

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

 

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?

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

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 derelektrischemoench
      Hi guys,
      I'm facing a somewhat strange issue here which I can't quite wrap my head around. 
      I have a PW site in development which runs on three machines simultaneously, one staging server which is accessible as a preview instance for my customer, my PC and my laptop. 
      I have three completely identical settings on each of the three machines (same apache version, same php version, same codebase, same database); however on my PC I am unable to log into the backend. I get no error message or anything, when I try to login; i just get redirected to the login  page. I have already enabled database driven sessions (I enabled them on my laptop, then I dumped the database and copied it to my pc); I have cleared the cache directory; I cleared the sessions in the database; I cleared my browser caches, I tried different browsers, all to no avail; I am unable to login when using my pc, the instances all have the same .htaccess.
      Is there something I'm missing here or does anyone have a clue as to what my issue here might be? I'm using processwire 3.0.123
      Thanks for any input, greetings
      derelektrischemoench
       
      //edit: I've noticed something interesting; despite the directories of my web folders being the same layout; when I open the admin page i get a 404 on the processwire/ resource in the networks panel of chrome; on my laptop I get a  200.... I guess this is where my problem is; but why?
       
       
    • By derelektrischemoench
      Hi guys,
      I'm facing a somewhat strange issue here which I can't quite wrap my head around. 
      I have a PW site in development which runs on three machines simultaneously, one staging server which is accessible as a preview instance for my customer, my PC and my laptop. 
      I have three completely identical settings on each of the three machines (same apache version, same php version, same codebase, same database); however on my PC I am unable to log into the backend. I get no error message or anything, when I try to login; i just get redirected to the login  page. I have already enabled database driven sessions (I enabled them on my laptop, then I dumped the database and copied it to my pc); I have cleared the cache directory; I cleared the sessions in the database; I cleared my browser caches, I tried different browsers, all to no avail; I am unable to login when using my pc, the instances all have the same .htaccess.
      Is there something I'm missing here or does anyone have a clue as to what my issue here might be? I'm using processwire 3.0.123
      Thanks for any input, greetings
      derelektrischemoench
       
       
    • By Peter Knight
      How do you guys handle large session tables when sessions are being recorded to the database?
      I notice one of my sites has a session table of over 14MB 
      Am I missing a way in the Admin or a module to auto-remove any sessions older than X days?
      Thanks
       
    • By helmut2509
      In my PW-Application there is currently no session timeout.
      I want to set the user session to 60 minutes which means that after 60 minutes of inactivity the user will be redirected to the homepage.
      so I added the following entry to my config.php:
      $config->sessionExpireSeconds = 120; (120 seconds is just for testing).
      But after five minutes of inactivity I am still logged in, there is no redirection.
      Is there anything wrong or did I miss something?
      In php.ini I have the entry:
      session.cookie_lifetime = 3600
    • By celfred
      Hello,
      I'm facing a weird issue here. I have a page loaded with this code inside (my comments in line ends) :

      if ($session->allPlayers) { // Set in a head.inc file. I have also a $session->set('allTeams', $allTeams); in my head.inc   $allPlayers = $session->allPlayers; } else {   $allPlayers = getAllPlayers($user, false);   $session->set('allPlayers', $allPlayers); } bd($session->getAll()); // HERE, I get a number of 11 variables which is what I expect In the same page, I have a link pointing to ajaxContent.php that loads stuff via Ajax.
      I just write this in my ajaxContent.php to test :

      bd($session->getAll()); // HERE, I get only 9 variables. All my newly set $session variables ($allTeams and $allPlayers) are not conveyed to ajaxContent.php ??? Would you have any idea why is that ??? Another thing : I have a $session->headMenu set in my head.inc, and this one works fine. I can retrieve it in my ajaxContent.php page.
      I've tried cleaning all caches but it doesn't change anything 😞 
      At first, I expected it to be a 15-minute update to my site... It turns out to be a 2-hour issue and I'm still  stuck.
      Thanks for your ideas ! 
×
×
  • Create New...