Programmatically logout user without redirect I have the following code in my module:

&lt;?php

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


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() &amp;&amp; request_uri() == '/user') {
    user_logout();
}


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.
You need to change your hook code.

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


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.