Jump to content
Martin Muzatko

user->isLoggedin() lags behind $session->login()

Recommended Posts

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.

Best,
Martin

Share this post


Link to post
Share on other sites

Can you post your entire code (or at least the relevant bits)?

3 hours ago, Martin Muzatko said:

Is a redirect really required to complete the session handling? Why?

That's simply how sessions work. You can't set new session vars and retrieve them in one go.

From http://php.net/manual/en/features.sessions.php

Quote

Session support in PHP consists of a way to preserve certain data across subsequent accesses.

The keyword being "subsequent" :-)

 

  • Like 2

Share this post


Link to post
Share on other sites

Hello. Thanks for your reply.

I know that sessions are supposed to work like that. I only wonder why the $user variable is only populated after a complete refresh. After all, after a successfull $session->login, I would expect $user->isLoggedIn() to return true. Even without a page refresh.

 

I wanted to display a little welcome message, or logout message, but I think I can also achieve this by redirecting him to another page. 

This is my current code - with the redirect in place.

 

<?php namespace ProcessWire;
    if (count($input->post()->getArray())) {
        $loginUser = $session->login($input->post->username, $input->post->password);
        if($loginUser instanceof User) {
            $session->redirect($pages->get('/')->url.'users/'.$loginUser->name);
        }
    }
    if($user->isLoggedin()) {
        $session->redirect($pages->get('/')->url.'users/'.$user->name);
    }
?>
<form action="" method="POST">
    <input type="text" name="username" autocomplete autofocus value="<?=$input->post->username?>">
    <input type="password" name="password" autocomplete value="">
    <input type="submit" value="Login">
</form>

 

Share this post


Link to post
Share on other sites

After verifying the login, you can do

$users->setCurrentUser($loginUser);

Afterwards, $user->isLoggedin() etc. should behave as expected. Setting the user "guest" as the current user (that part hasn't been tested by me) should accomplish the same after a logout.

  • Like 3

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 prestoav
      Hi there,
      I have a marketplace site that used the older Fredi module for front end editing so advertisers can create and edit their adverts (in the form of pages). After upgrading to v3.0 of Processwire I'd like to change the editing method for users to the new, core front end editing methods. However, it seems that with this method any editor can edit any page (subject to template restrictions for their user role). Using Fredi I checked the user and created user were equal before displaying edit buttons (clunky but it worked).
      Is there any stock way to restrict front end editing to the creator of the page so that my advertisers can only edit their own ads or should I test for user as before?
      Thanks in advance as always 🙂
×
×
  • Create New...