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 alejandro
      Hello, after creating an array and assigning it to a variable in session:
      $order = array (); $order['token'] = 'token'; $order['product'] = 'product-name'; $session->order = $order; I try to insert another item:
      $session->order['price'] = 'price'; But it doesn't work: 
      Notice: Indirect modification of overloaded property ProcessWire\Session::$order has no effect in... Isn't possible to modify such session variable? I could build another array from the session variable, add the new item, and then store it again in session, but doesn't looks good.
      Thanks!
    • By dragan
      If I have two PW sites that sit in separate folders, I can't be logged-in in both sites.
      e.g.
      site.com/project-a/pw-admin-slug/
      site.com/project-b/pw-admin-slug/
      If I login to project-a, then also login to project-b, get back to the first site, I have to login again.
      Is the cookie / session mechanism storing my domain? If it does, and it's meant to be some sort of security enhancement, it should not check my domain, but root-URL of the PW-installation. (strangely, this doesn't happen on localhost)
      Is it possible to prevent that behavior? Often I have two sites open (e.g. check to see if I have the same CKEditor setup and quickly copy and paste it, or copy a user-role)
    • By Jarden Black
      Hi everyone,
      [edit: do not loose your time reading this post, I solved it by disabling cache in the Pages2Pdf module... sorry 😓]
       
      I do not know if I should post on the Pages2Pdf thread or here. Mods, feel free to move the post.
       
      Since three days I am scratching my head  to understand a weird thing happening with $session and $config->debug used in conjunction with Pages2Pdf module. For information, I tested it on a fresh install of ProcessWire 3.0.96 with PHP-7.0.28 and Pages2Pdf-1.1.7 (domain: http://session.sites.sek/). I will try to explain what is going on.
       
      What I am trying to achieve :
      In a template, I need to set some sessions variables which are then echo'd in the PDF document.
      (on the test installation, the basic-page template (page /about/?pages2pdf) serve the PDF, the home and sitemap template set the session variable.)
       
      The problem :
      From the template sitemap, I set a variable: $session->setFor('pdf', 'myvar', 'Session set from Sitemap template');
      From the template home, I set a variable: $session->setFor('pdf', 'myvar', 'Session set from Home template');
      Then in the PDF default template, I echo the session variable: <p>Session: <?= $session->getFor('pdf', 'myvar'); ?></p>
       
      Now I turn ON debug mode ($config->debug = true) :
      Then I navigate to  "http://session.sites.sek/home/" and the session variable "myvar" is set to "Session set from Home template". Then I navigate to  "http://session.sites.sek/sitemap/" and the session variable "myvar" is set to "Session set from Sitemap template". Now I want my PDF document, so I navigate to "http://session.sites.sek/about/?pages2pdf=1" and I get my PDF document with the right session var : "Session set from Sitemap template" For the moment, nothing special happen. Everything work great. We are in debug mode.
       
      Now I turn OFF debug mode ($config->debug = false) :
      Then I navigate to  "http://session.sites.sek/home/" and the session variable "myvar" is set to "Session set from Home template". Then I navigate to  "http://session.sites.sek/sitemap/" and the session variable "myvar" is set to "Session set from Sitemap template". Then I navigate back to "http://session.sites.sek/home/" and the session variable "myvar" is set back to "Session set from Home template". Now I want my PDF document - as expected, the "myvar" should be set to "Session set from Home template" - so I navigate to "http://session.sites.sek/about/?pages2pdf=1" and here the problem happen. Instead of echoing  "Session set from Home template" in the PDF document, the phrase "Session set from sitemap" is echo'd (the last value recorded before switching from debug ON).  
       
      I made two small screencasts to show the issue :
      DEBUG ON (Everything is OK)
       
      DEBUG OFF
       
       
      I am missing something ? EDIT: YES, you are dumb!
       
       
    • By flydev
      Hi everyone,
      [edit: do not loose your time reading this post, I solved it by disabling cache in the Pages2Pdf module... sorry 😓]
       
      I do not know if I should post on the Pages2Pdf thread or here. Mods, feel free to move the post.
       
      Since three days I am scratching my head to understand a weird thing happening with $session and $config->debug used in conjunction with Pages2Pdf module. For information, I tested it on a fresh install of ProcessWire 3.0.96 with PHP-7.0.28 and Pages2Pdf-1.1.7 (domain: http://session.sites.sek/). I will try to explain what is going on.
       
      What I am trying to achieve :
      In a template, I need to set some sessions variables which are then echo'd in the PDF document.
      (on the test installation, the basic-page template (page /about/?pages2pdf) serve the PDF, the home and sitemap template set the session variable.)
       
      The problem :
      From the template sitemap, I set a variable: $session->setFor('pdf', 'myvar', 'Session set from Sitemap template');
      From the template home, I set a variable: $session->setFor('pdf', 'myvar', 'Session set from Home template');
      Then in the PDF default template, I echo the session variable: <p>Session: <?= $session->getFor('pdf', 'myvar'); ?></p>
       
      Now I turn ON debug mode ($config->debug = true) :
      Then I navigate to  "http://session.sites.sek/home/" and the session variable "myvar" is set to "Session set from Home template". Then I navigate to  "http://session.sites.sek/sitemap/" and the session variable "myvar" is set to "Session set from Sitemap template". Now I want my PDF document, so I navigate to "http://session.sites.sek/about/?pages2pdf=1" and I get my PDF document with the right session var : "Session set from Sitemap template" For the moment, nothing special happen. Everything work great. We are in debug mode.
       
      Now I turn OFF debug mode ($config->debug = false) :
      Then I navigate to  "http://session.sites.sek/home/" and the session variable "myvar" is set to "Session set from Home template". Then I navigate to  "http://session.sites.sek/sitemap/" and the session variable "myvar" is set to "Session set from Sitemap template". Then I navigate back to "http://session.sites.sek/home/" and the session variable "myvar" is set back to "Session set from Home template". Now I want my PDF document - as expected, the "myvar" should be set to "Session set from Home template" - so I navigate to "http://session.sites.sek/about/?pages2pdf=1" and here the problem happen. Instead of echoing  "Session set from Home template" in the PDF document, the phrase "Session set from sitemap" is echo'd (the last value recorded before switching from debug ON).  
       
      I made two small screencasts to show the issue :
      DEBUG ON (Everything is OK)
       
      DEBUG OFF
       
       
      I am missing something ? EDIT: YES, you are dumb!
      Why it is working with debug mode ON and not vice-versa ?
      Is there someone who already spotted this strange behavior ?
      Is there a PHP settings which should be modified ?
       
      ====================================================
      Edit:
      I needed to post just to figure myself that Pages2Pdf cache the document when $config->debug is false.
      😬😬😬
       
    • By eutervogel
      Hi,
      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