
Sep 09
Determing Login State (MODx)
With MODx only recently being released as a full version (1.0) a lot of the documentation is out of date and a few plugins/snippets fail to work. After hunting around, playing a bit and editing some snippets this did the trick. To create a new snippet go into the MODx manager > Elements > Manage Elements > Snippets > New Snippet. Add “LoginState” as the snippet name, add a useful description and paste in the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php // Thanks to however highlighted the inside manager part if ($m = $modx->insideManager()) { return ''; # don't go any further when inside manager } $output = ''; $state = $_SESSION['webValidated']; if ($state == 1) { $output = '<a href="user.html?service=logout" title="Logout" class="logout">Logout</a>'; } else { $output = '<a href="user.html" title="Login">Login</a>'; } return $output; ?> |
Save the snippet. Edit your template (where you want to output whether a person is logged in or not) and simply add:
1 | [!LoginState!] |
I know this is extremely simple and straight forward but I couldn’t find any documentation and didn’t have much time to dig around looking at the framework/API. The script above isn’t flexible, obviously, but can be extended with the session var “webValidated”.







Tom 22 October 2009 6:47 am
Thank you. I needed similar functionality to show a ‘Logout’ link on my page and also an ‘Update Profile’ link. I ended up creating a snippet similar to yours above.
As an alternative, though, I tried to use a template variable (tv), which did work so long as your pages that use it in aren’t cached.
For instance, I created a tv called ‘manageUser’:
Input Type: text
Default Value: @EVAL if (isset($_SESSION['webValidated'])) return ‘[~76~]‘; else return ”; (document ID [~76~] is the page that displays the user’s profile by calling the WebLoginPE snippet: [!WebLoginPE? &type=`profile` !])
Widget: Hyperlink
Display Text: Update Profile
Then I use the ‘manageUser’ tv in my template:
…
[*manageUser*]
Effectively, I have a footer at the bottom of every page that shows a link ‘Update Profile’ if you’re logged in and nothing if you’re not.
Again, if the page that uses this template has caching turned on, then the tv is evaluated once and cached, so its value never changes if you login/logout. Major bummer! I didn’t want to turn off caching for all my pages, so I went the uncached snippet route.
Gibbs 23 October 2009 11:18 am
Thanks for that Tom, very helpful.
Much appreciated.
Rhepungus 5 February 2010 5:54 pm
Thanks guys, you really helped me out today. Much appreciated.