We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21122
    • 153 Posts
    Hi there,

    I’m using the login snippet for my site, which cannot be accessed by anyone who isn’t logged in. Thats all working nicely, but I want to be able to redirect users who are not logged in (and try to access a secure area of the site) to the login form.

    At the moment, when I try going to an unauthorized page without being logged in, I just get a blank page, and when I look at the source, it looks like its tried to load one of the pages, but stops after loading several lines of the code (it usually stops somewhere in the head tag during my .js script references).

    I tried adding a snippet to the top of each of my templates which reads:

    <?php
    if (!userLoggedIn()) {
       $url = $modx->makeUrl(1);
       $modx->sendRedirect($url);
    }


    But this doesn’t seem to be working.

    Any ideas?!

    Thanks
      • 25308
      • 46 Posts
      Hi Neil,

      It sounds like you’ve set up Resource groups and assigned your pages to them.

      The login package has an automatic redirection to a specified page in the system settings. You won’t need any additional php.

      You can set this by going to:

      System > System settings > and typing "Unauthorized" in the in the filter. The number listed in the value is the page that all users that are not loggeg in will be redirected to. Just change that value to the page # of your login page.

      Just a side note though you may need to add a "You will have to log in to view member content" message somewhere on your login page. I had some confusion with some of my member when I fist did this.

      I actually later just created an "You must login to view member content" page with links to the home and login pages.

      I hope this helped you. cool

      RL


      http://rtfm.modx.com/display/ADDON/Login
        • 3749
        • 24,544 Posts
        Your snippet is a good idea, but if you’re using Revolution, it should be:

        if (!$modx->user->hasSessionContext('web') )  {
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 9207 ☆ A M B ☆
          • 2,475 Posts
          Just for the record, the tutorial in the docs covers the snippets, user groups, and resource groups you’ll need:
          http://rtfm.modx.com/display/ADDON/Login.Basic+Setup