If you have a site where the home page is a login to the rest of the site, and all other pages require a login, is there a way to do this without adding every resource to a resource group?
-
- 24,544 Posts
Yes, you could put this snippet in your template for every page but the home page (this would be for MODX Revolution). It will forward any not-logged-in user to the home page (untested):
/* RequireLogin snippet */
if (! $modx->user->hasSessionContext($modx->context->get('key'))) {
$url = $modx->makeUrl($modx->getOption('site_start'), "", "", "full");
$modx->sendRedirect($url);
}
return '';
This would be a little faster if you only have the web context and won't be changing the site_start page:
/* RequireLogin snippet */
$homeId = 12; // ID of home page
if (! $modx->user->hasSessionContext('web')) {
$url = $modx->makeUrl($homeId, "", "", "full");
$modx->sendRedirect($url);
}
return '';
[ed. note: BobRay last edited this post 11 years, 3 months ago.]
Thank you. Second one worked wonderfully. This would be a good snippet for anyone who has a fully protected site.
First one did not redirect but just presented no content (and no HTML code at all) to a user visiting a page directly though we definitely have the site_start setting pointing to the login page. If one has a default install and only one context (web) is a key for the context even set or would it have to be set manually?
-
- 24,544 Posts
The first version will work on any Context to see if the user is logged in to that Context. It had a typo (fixed above), but if the 'web' context is the only front-end context the site will ever have, there's no need for it.
In MODX, $modx->context->get('key') will always return something (by default 'mgr' or 'web').
Great. First version functions now as well.