We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17930
    • 81 Posts
    I wrote this in Revo 2.0.8.

    This may be a silly post, but hey.

    I wrote a snippet that when a person is logged in, it will not run/record the analytics. I just didn’t want the analytics messed-up from me changing and looking around the website. I do a lot of changing to all the pages and wanted to see just the pure internet users and not have my browsing the site corrupt the data.

    I actually created a chunk that called the snippet, just incase I wanted to put the analytics back into the chunk to record everything. The snippet must be called un-cached. This is very important.

    (see updated code in post #3)
    <?php
    // Gets the analytics if the person is not logged in
    // Must call un-cached.
    // [[!Get_Analytics]]
    	
    	$output = '';
    	$viewer_logged_in = false;
    	
    	$viewer = $modx->user;
    	
    	if( $viewer->get('id') != null )
    		$viewer_logged_in = true;
    	if($viewer_logged_in == false)
    	{
    		$output = 'Put your analytics here, but you will have to escape the apostrophes \' .';
    	}
    	else
    	{
    		$output = 'You Are Logged In';
    	}
    	
    	return $output;


    There may be a easier way to do this, but this is what I came up with.

    Also, I’m still learning Revo, so I can’t figure out what to use/look for in order to tell the difference between some one logged in on the front-end as opposed to the back-end. And if they are a manager user or just a web user. If I find a way later, I’ll share it, because it may be good sometimes to track the analytics of a web user.

    Searched:
    hide analytics
    not show analytics while logged in
      • 3749
      • 24,544 Posts
      It’s a great idea, and it should also speed up work in the Manager. cool

      You might try this:

      $viewer_logged_in = $modx->user->hasSessionContext($modx->context->get('key'));


      And I would change $return to $output, since that’s a fairly universal MODx convention.
        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
        • 17930
        • 81 Posts
        Thanks Bob. That works great. Below is the code.

        I tried doing using an array for the contexts, but couldn’t get it to work.

        $viewer_logged_in = $modx->user->hasSessionContext( array('mgr', 'web') );



        So I just did this instead. (Sometimes I tend to go overboard on the simple code.)

        <?php
        // Gets the analytics if the person is not logged in
        // Must call un-cached.
        // [[!Get_Analytics]]
        	
        	$output = '';
        	
        	//BobRay from bobsguides.com suggested using this.
        	$mgr_logged_in = $modx->user->hasSessionContext( 'mgr' );
        	$web_logged_in = $modx->user->hasSessionContext( 'web' );
        	
        	if($mgr_logged_in == false && $web_logged_in == false)
        	{
        		$output = 'Put your analytics here, but you will have to escape the apostrophes \' .';
        	}
        	else
        	{
        		$output = 'You\'re logged in as ';
        		
        		if($mgr_logged_in == true && $web_logged_in == true)
        			$output .= 'an Admin and User.';
        		elseif($mgr_logged_in == true && $web_logged_in == false)
        			$output .= 'an Admin.';
        		elseif($mgr_logged_in == false && $web_logged_in == true)
        			$output .= 'a User.';
        	}
        	
        	return $output;


        I haven’t tested out that it would work for web logged in.
          • 17930
          • 81 Posts
          It does work great, until you create a Context for use as a subdomain. Then it doesn’t recognize that I’m logged into the manager.

          Here is where I tested the different contexts.

          $mgr_logged_in = $modx->user->hasSessionContext( 'mgr' );
          $web_logged_in = $modx->user->hasSessionContext( 'web' );
          if($web_logged_in == false)
          	$web_logged_in = $modx->user->hasSessionContext( 'whatiscss' );


          When I go to mysite.com, it will show that I’m logged into the manager, but when I go to whatiscss.mysite.com, it doesn’t recognize that I’m logged into the manager.
            • 3749
            • 24,544 Posts
            Quote from: mafokken at Feb 26, 2011, 11:51 PM

            It does work great, until you create a Context for use as a subdomain. Then it doesn’t recognize that I’m logged into the manager.

            Here is where I tested the different contexts.

            $mgr_logged_in = $modx->user->hasSessionContext( 'mgr' );
            $web_logged_in = $modx->user->hasSessionContext( 'web' );
            if($web_logged_in == false)
            	$web_logged_in = $modx->user->hasSessionContext( 'whatiscss' );


            When I go to mysite.com, it will show that I’m logged into the manager, but when I go to whatiscss.mysite.com, it doesn’t recognize that I’m logged into the manager.

            Have you tried the code I posted in my original message? I don’t see it in any of your examples. It tests using the name of the current context, whatever it is. If that doesn’t work when previewing from the manager, you could "or" it with your a test for being logged into the mgr context.
              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
              • 17930
              • 81 Posts
              I did try the code you gave me, but couldn’t figure it out. I kind of wish there was an Modx for dummies. There is probably so much wrong with the code I write. smiley (I do wish the Revo API had examples like the old API did. I know, though, that the API is huge and was a huge project to do, thank you Modx team.)

              I tried these, but I couldn’t find a difference. I couldn’t even get it to output, except with print_r, but that isn’t right, is it?
              $modx->user->hasSessionContext($modx->context->get('key'))
              $modx->user->hasSessionContext($modx->context->get('mgr'))
              $modx->user->hasSessionContext($modx->context->get('web'))
              $modx->user->hasSessionContext($modx->context->get('adsfadsf'))


              When I’m logged into the manager and go to a subdomain, which is a different context than web, should I be able to tell if I’m logged into the manager on the subdomain?
                • 3749
                • 24,544 Posts
                Sorry, I should have mentioned that hasSessionContext() returns true if the user is logged in and false if not.

                The first one of your examples checks to see if the user is logged in in the current context, whatever it is.

                $modx->context->get(’key’) just returns the name of the current context (e.g. ’web’ ’mgr’ or whatever).

                I think this will determine is the user is logged into either context:

                $modx->user->hasSessionContext('web,mgr');


                Remember that some users will create other contexts as well, so this should tell if a user is logged in anywhere:

                <?php
                if ($modx->user->hasSessionContext('web,mgr') || $modx->user->hasSessionContext($modx->context->get('key')) ) {
                     /* user is logged in somewhere */
                }


                Because PHP evaluates from left to right and it’s an OR statement, if the left half is true, PHP won’t bother to evaluate the right half.
                  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
                  • 17930
                  • 81 Posts
                  Okay, so I can figure out if I’m logged into the manager and/or the web context.

                  However, I can’t seem to see that I’m logged into the manager when I’m at a context other than web. Shouldn’t I be able to tell?

                  I do appreciate your help. I guess I just want to be able to see exactly where the user is logged in.
                    • 3749
                    • 24,544 Posts
                    Quote from: mafokken at Mar 02, 2011, 01:06 AM

                    Okay, so I can figure out if I’m logged into the manager and/or the web context.

                    However, I can’t seem to see that I’m logged into the manager when I’m at a context other than web. Shouldn’t I be able to tell?

                    I do appreciate your help. I guess I just want to be able to see exactly where the user is logged in.

                    If you’re previewing the front end from the Manager, I think you have special status, but I’m not sure exactly what that means. tongue
                      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