I need to detect if I'm currently browsing the site as a different user, having used the devel user switch module.

The LDAP module I have triggers some profile updates on every login, based on some server variables set from AD/SSO. But by using devel switch, it's writing MY user details, to the profile of the person I've switched to.

I need a way of detecting it, so I can prevent the profile write.

I've had a look through the devel API here but can't see that anything is set when running as another user.

Running D7.

1 Answer 1


The Devel switch feature passes the login straight to the user module in devel_switch_user(); it doesn't give other modules the chance to intervene, and doesn't provide any context that would let you know how the login has been instigated.

I guess as a simple low-tech solution you could patch that function and set a session var before it invokes the login, something like

if (!$old_uid) {
  // Switch from anonymous to authorized.

$_SESSION['login_from_devel_switch'] = TRUE;

$edit = array();
user_module_invoke('login', $edit, $user);

Then you can check for the existence of that session var wherever you need to prevent the profile from writing.

  • Seems like it would work. I'll need to check to see if hitting "logout" clears the session variable, if not I'll need to add that in too. Commented Oct 31, 2013 at 15:57
  • It does - user_logout() calls session_destroy(). Wouldn't be much of a logout if it left session vars ;)
    – Clive
    Commented Oct 31, 2013 at 16:02
  • Ha... good point! Nice simple solution then Clive, thanks for the help. Commented Oct 31, 2013 at 16:19
  • 1
    I'll submit a feature request once drupal.org is back up to get this added to the devel module. I'm sure other's will find it handy! Commented Oct 31, 2013 at 16:20
  • That's a great idea. Maybe the introduction of a hook would be the best way to go? Then there's scope to do more than just set a session var - for example you could track which users are switching to who and when, and loads of other things
    – Clive
    Commented Oct 31, 2013 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.