On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • I'm using the login snippet with a post hook that redirects a user to a personal landing page.
    This is set up and working with this code:

    <?php
    $username = $hook->getValue('username');
     
    $pageId = '7';
      
    switch ($username) {
      case 'BossJudge':
              $pageId = '23';
              break;
      case 'Myla72':
              $pageId = '24';
              break;
      case 'TwoBuses':
              $pageId = '25';
              break;
      case 'Trainsporter01':
              $pageId = '26';
              break;
      case 'JonesJazz':
              $pageId = '27';
              break;
      case 'ShipperMein':
              $pageId = '37';
              break;  
      case 'guangun91':
              $pageId = '40';
              break;           
    }
     
    $landingPage = $modx->makeUrl($pageId, "", "", 'full');
    $modx->sendRedirect($landingPage);


    If someone enters an incorrect username or password then the usual error message appears.

    Where I'm getting problems is if the correct username is used but with the wrong case for letters, then the user appears signed in but is redirected to the Log Out page.

    for example; correct username = 'meuser' this goes to the correct landing page.
    But.....
    If I try this username ='MEUSER' it does not display an error, it just sends the user to the logout page.

    Do I need to change the word 'case' in the above snippet?
    If yes change to what?
    Or is there some other mischievous piece of code I've not yet identified that I must change?

    Can anyone advise?

    This question has been answered by BobRay. See the first response.

    • Try this:

      switch (strtolower($username)) {
        case 'bossjudge':
                $pageId = '23';
                break;
        case 'myla72':
                $pageId = '24';
                break;
        case 'twobuses':
                $pageId = '25';
                break;
        case 'trainsporter01':
                $pageId = '26';
                break;
        case 'jonesjazz':
                $pageId = '27';
                break;
        case 'shippermein':
                $pageId = '37';
                break;  
        case 'guangun91':
                $pageId = '40';
                break;           
      }
      
        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
      • Quote from: BobRay at Aug 26, 2015, 08:11 PM
        Try this:

        switch (strtolower($username)) {
          case 'bossjudge':
                  $pageId = '23';
                  break;
          case 'myla72':
                  $pageId = '24';
                  break;
          case 'twobuses':
                  $pageId = '25';
                  break;
          case 'trainsporter01':
                  $pageId = '26';
                  break;
          case 'jonesjazz':
                  $pageId = '27';
                  break;
          case 'shippermein':
                  $pageId = '37';
                  break;  
          case 'guangun91':
                  $pageId = '40';
                  break;           
        }
        

        Hi Bob,

        Thanks for your input, I added this line only (because the usernames use both upper & lower case):

        switch
         (strtolower($username)) {


        The result is if a user logs in they go to the correct landing page regardless of case used.

        This is acceptable because it stops users being sent to log out page.

        However, when I added the above line it now throws a syntax error:

        syntax error, unexpected '{'

        The code gives the functionality I require but I'm unsure how to clear this error.
        • Actually I only tested it on one username and it seemed to work.

          I just tried it in a different browser & it is sending everyone to the log out page.

          I've just changed it back to the original so it is back to the case sensitive issue.
          • discuss.answer
            Sorry, this should work:

            $name = strtolower($username);
            switch ($name) {
              case 'bossjudge':
                      $pageId = '23';
                      break;
              case 'myla72':
                      $pageId = '24';
                      break;
              case 'twobuses':
                      $pageId = '25';
                      break;
              case 'trainsporter01':
                      $pageId = '26';
                      break;
              case 'jonesjazz':
                      $pageId = '27';
                      break;
              case 'shippermein':
                      $pageId = '37';
                      break;  
              case 'guangun91':
                      $pageId = '40';
                      break;
              default:
                      break;           
            }


            Replace the whole switch statement with this code.
              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
            • Quote from: BobRay at Aug 27, 2015, 04:59 AM
              Sorry, this should work:

              $name = strtolower($username);
              switch ($name) {
                case 'bossjudge':
                        $pageId = '23';
                        break;
                case 'myla72':
                        $pageId = '24';
                        break;
                case 'twobuses':
                        $pageId = '25';
                        break;
                case 'trainsporter01':
                        $pageId = '26';
                        break;
                case 'jonesjazz':
                        $pageId = '27';
                        break;
                case 'shippermein':
                        $pageId = '37';
                        break;  
                case 'guangun91':
                        $pageId = '40';
                        break;
                default:
                        break;           
              }


              Replace the whole switch statement with this code.

              Hey Bob,

              That works great smiley

              Cheers mate, one less headache , now onto the next challenge.
              • I'm glad I could help. smiley
                  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