This seems like a simple problem, but I can’t find any documentation anywhere.
This covers two scenarios:
1. A webuser is not logged in and tries to access a protected page.
2. A webuser’s session expires, and they refresh or try to navigate to a protected page.
When either situation occurs, the site simply redirects to the page specified in the "Unauthorized Page" system setting in the manager. I understand you can create a custom page that will let them know that they were redirected because of unauthorized access, but what if you just want to redirect them to the home or login page? Is there a way to set/retrieve a message that can be displayed dynamically on that page?
Thanks in advance.
You could change the unauthorized page to be and do whatever you want, but I would probably use the Personalize snippet, which shows one chunk to logged-in-users and another to not-logged-in users.
Quote from: dansig at Mar 23, 2010, 04:11 PM
Quote from: BobRay at Mar 23, 2010, 03:52 PM
You could change the unauthorized page to be and do whatever you want, but I would probably use the Personalize snippet, which shows one chunk to logged-in-users and another to not-logged-in users.
Thank you Bob, but I’m trying not to use a custom unauthorized access page, but rather, redirect to an existing page, like home, and just display a dynamic message. The personalize snippet will not work for this.
Maybe there is no way around it. 
OK. How about this?
Use the Personalize snippet on the home page, to show your message only to anonymous users, then add a snippet with this code to the pages you want protected:
if (!getAuthenticatedUser()) {
$id = 12; // id of the page you want to send them to
$modx->sendRedirect($modx->makeURL($id));
}
The code’s off the top of my head, so it may not be exactly right. You could also save a message in a $_SESSION variable in the code above and show it with a snippet on the landing page.
Okay, I see what you are getting at, but how about this as an easier way to do it.
Instead of adding a snippet to every authenticated page and using personalize, add the snippet to the custom "unauthorized access" page, set the session variable there, and then redirect to the home page where you simply echo the session variable. (I already have a class written called "Console" that is a session-based messaging system)
It’s a little roundabout way to do it, but I think it will work. Thanks for the ideas!
I don’t see anything wrong with that and it’s probably more efficient than running an extra logged-in check on every page.
Glad you got it sorted. Thanks for reporting back.