We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38553
    • 12 Posts
    I used PageLocker for a customer's site now he wants me to add a logout button.
    For this I added a button that calls a logout.php with following code:
    <?php 
         session_start(); 
         unset($_SESSION['password']); 
         session_destroy(); 
         session_write_close(); 
         header("Location: /"); 
         die; 
         exit; 
    ?>


    This code does not end the session. When I try to call the site after logging out I can reach it without logging in. I am not sure what is wrong.
    I also tried it with stating no session name and got the same result.

    Does anybody know what to do?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 38553
        • 12 Posts
        Thank you for your answer. I am not sure whether this logout function is for PageLocker or not but it doesn't seem to work. I think this is for normal MODX logins not for pages which are locked by PageLocker. Am I right?

        Effect by using this is that i am redirected to login page by pressing logout button. After entering the locked page's url it is shown without the need to login.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Ah. I presumed that PageLocker required a user to be logged in.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 38553
            • 12 Posts
            Does nobody know how to do this? I really need help with this.
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Well, looking at your code, you don't start a session in MODX snippets. MODX already has its own session that is being used. I'm not at all familiar with just what PageLocker is doing, but if it sets a SESSION value for its own status, and you know what it is, all you need to do is unset that SESSION value.

              I'll check it out and see if I can figure out exactly what SESSION value you need to unset. You definitely do not want to start another session, nor do you want to close the existing MODX session.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                Looks like you just need to unset $_SESSION['loggedin'], and maybe $_SESSION['groups'] and $_SESSION['password']. I would also be inclined to redirect to some specified page.
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 38553
                  • 12 Posts
                  Got it to work. Following works für me:

                  <?php
                       session_start();
                       unset($_SESSION['loggedin']);
                       unset($_SESSION['groups']);
                       unset($_SESSION['password']);
                       if (ini_get("session.use_cookies")) {
                           $params = session_get_cookie_params();
                           setcookie(
                               session_name(), 
                               '', 
                               time() - 42000,
                               $params["path"], $params["domain"],
                               $params["secure"], $params["httponly"]
                           );
                       }
                       session_destroy();
                       session_write_close();
                       header("Location: /");
                       $_SESSION['loggedin'] = 0;
                       die;
                       exit;
                  ?>
                  


                  And the link to logout.php:
                  <button class="shortcode_button btn_small"><a href="[[++site_url]]logout.php" title="Logout">Logout</a></button>
                    • 38553
                    • 12 Posts
                    Ok, now I have another problem. Logout works perfectly but if someone presses browser's back button it logs in automatically. I am not sure how to solve this.
                    For example Crome asks to send the form again.

                    Does anybody know how to prevent this?
                      • 36551
                      • 416 Posts
                      I'm wondering if you were able to resolve this. My client doesn't want the pw to appear in the field when they return to the page.

                      Any help would be greatly appreciated.