I have the following code in my module:


function MODULENAME_init() {
        if (!user_is_logged_in()) {
                global $user;
                $user = user_load(31);

Purpose of this code is to log in all anonymous user as user with id 31. But with this code i can't even logout correctly:

if (user_is_logged_in() && request_uri() == '/user') {

After i am visiting http://ggames.kz/user i am getting redirected back to the main page and so i am logged again as user 31. I want to log out any user when he's requesting http://ggames.kz/user and show a login form in order to log in as user with id 1.

  • I guess, your code in hook_init is the reason you can't log out. Because once you logout, it log in you back. Commented Feb 2, 2016 at 6:21
  • 1
    The really interesting question is, why the auto-login is necessary? Maybe it's possible to find a better solution for this, once being able to understand your intentions on auto-login. Commented Feb 2, 2016 at 6:53
  • Also, notice that ggames.kz/user is used from users to access their user page.
    – apaderno
    Commented Feb 2, 2016 at 6:54

1 Answer 1


You need to change your hook code.

function MODULENAME_init() {
  if (!user_is_logged_in() && request_uri() != '/user') {
    // Log in the user as user #31.
  elseif (user_is_logged_in() && request_uri() == '/user') {
    module_load_include('pages.inc', 'user');

Since user_logout() is defined in modules/user/user.pages.inc, you need to first load that file, and then call that function, or you will get an error about a not defined function. You don't need to first check the file has been already included, since it will not be included twice.

Keep in mind that http://ggames.kz/user could be directly accessed from users to see their user page, and that Drupal redirect logged-in users who try to access http://ggames.kz/user/login to http://ggames.kz/user.

  • Thanks, i am awared about this. Now when i visit ggames.kz/user (first time it loads as expected - auth form) second time - i am getting an error "Fatal error: Call to undefined function user_logout()" Commented Feb 2, 2016 at 8:04
  • Right, I forgot the function is not defined in a module.
    – apaderno
    Commented Feb 2, 2016 at 8:08
  • @kiamlaluno Your load is off a bit, should be module_load_include('inc', 'user', 'user.pages');
    – Clive
    Commented Feb 2, 2016 at 8:52
  • @Clive Yep, I inverted two arguments. But I could also use module_load_include('pages.inc', 'user').
    – apaderno
    Commented Feb 2, 2016 at 9:41

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.