Sending a User to their Own Page on Login

How to get MODX to forward users to their own pages on login


MODX Forum user arra_amira asked for a way to forward users to their own page on login. In this article, we'll see a way that doesn't work and one that does.


MODX logo


Fail on My Part

I initially thought this would work:

Create a user setting called user_page and put the resource ID of the user's page as the value. Then, use this code in the Login tag to forward the user:

[[!Login? &loginResourceId=`[[++user_page]]` ]]

Can you see why it won't?

It's pretty obvious, though it wan't to me in the wee hours of the morning. Since the user hadn't logged in, MODX has no idea who the user actually is. As a result, there's no way for MODX to fill in the user_page setting tag for the property in the in the Login snippet tag.

After trying my flawed method, arra_amira worked out the correct solution.

This Works

Create the user setting (user_page) described above and put the ID of the user's page as the value. Here are the steps to do that:

  • Go to Manage -> Users in the Manager's Top Menu
  • Right-click on the user and select "Update User
  • Click on yhr "Settings" tab
  • Click on the "Create New" button
  • Fill in the Key, Name, Description, and put the ID of the user's page in the Value field
  • Click on the "Save" button at the bottom of the popup
  • Click on the "Save" button to save the user

Now, create a landing page resource called "Landing Page" with just this content:

   [[!ForwardUser]]

Make a note of the ID of the Landing Page, well use it in just a bit.

Now create a snippet called ForwardUser with this code:

$userPageId = $modx->getOption('user_page', null, $modx->getOption('site_start', null), true);
$url = $modx->makeUrl($userPageId, "", "", "full");
$modx->sendRedirect($url);
return '';

Finally, put this in your Login tag:

[[!Login? &loginResourceId=`12`]]

Replace 12 with the ID of the Landing Page

Users who log in successfully will be forwarded to the Landing Page where the ForwardUser snippet will forward them to their own page. If the User Setting doesn't exist or is empty, it will send the user to the site's Home (site_start) page.


What if I Want to Forward the User to Editing Their own Page?

I absolutely do *not* recommend this in most cases, but you might learn something useful from it. In order to use the Manager, the user has to be logged in there. You can forward the user to the Create/Edit Resource page for their own resource with this code in the ForwardUser snippet:

$modx->createSessionContext('mgr');
$url = MODX_MANAGER_URL . '?a=resource/update&id=' . $userPageId;
$modx->sendRedirect($url);

The technique above is not a good idea because many users have very simple passwords. A hacker could use a brute-force technique to log into the Manager as that user. The Login snippet, unlike the Manager login, does not block users after a certain number of failed login attempts.

You could edit the Manager's Login template to do this, but it would be fairly complicated. You'd have to duplicate all the manager template files (so your change wouldn't be overwritten during MODX upgrades), change the manager_theme System Setting, and do that over again if the Manager Templates are changed. In the next article, we'll look at a much better method for doing it.


Coming Up

In the next article, we'll look at sending a user to edit their own specific page when they log in to the Manager.



Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)



Comments (0)


Please login to comment.

  (Login)