1

I'm trying to customize login, logout functionality in my drupal site. My custom login function works fine by using the below code:

function my_module_custom_login() {
  $username = $_POST['username'];
  $pass = $_POST['password'];
  $uid = user_authenticate($username, $pass);
  $form_states = array('uid' => $uid);
  if(!empty($uid)) {
    $user_login = user_login_submit(array(), $form_states);
    return 'user is logged in';
  }
  else {
    return 'user is not logged in';
  }
}

After calling this function I checked the "Recent Log message" in my drupal reports it tells "Session open for user abc".

Here is my logout code:

function my_module_custom_logout() {
  module_load_include('inc', 'user', 'user.pages');
  user_logout();
}

Whenever I called this function the recent log message shows "Warning: session_destroy(): Trying to destroy uninitialized session in user_logout()" That call went as an anonymous call. So the current user can't be able to logout from the site. How can I overcome from this.

2

The way you've structured the logic, your function will return "user is logged in" as long as your credentials are correct, not if the user session was created (i.e. the user logged in). Your subsequent error about an uninitialized session confirms that a session isn't being created (i.e. an entry being inserted into the sessions table) or your session key/cookie isn't persisting on the subsequent logout call.

You need to debug why this isn't happening. Check the drupal_session_regenerate() function call to make sure a session entry is created and that your browser/client is receiving SetCookie header with the newly created session key. If the session is created, you need check why your browser/client isn't storing and persisting the session key made on your subsequent logout call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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