Error hook_user op not working?

I have the following code in a user module for saving session_id for comparison after login. I want to add it to the user object, so I called hook_user as follows:

function mymodule_init() {
    global $user;

    if ($user->uid == 0 && !isset($_SESSION['anonymous_session_id'])) {
        $_SESSION['anonymous_session_id'] = session_id();
    }
}

function mymodule_user($op, &$edit, &$account, $category = NULL) {
    switch ($op) {
        case 'load':
            $user->anonymous_session_id = $_SESSION['anonymous_session_id'];
            break;
        default:
            break;
    }
}

However, it is not in the user object. There is a "session" field that has a serialized array of $ _SESSION information, which would mean that I probably don't need hook_user, but why does this code not work?

+3
source share
1 answer

There are two problems you face:

  • The user object is hook_user()not in $user(this is not one of the parameters): it is actually in $account.
  • $user $account hook_user() (. ).

, :

global $user;
$account = user_load(array($user->uid));

, user_save(), , $user hook_user($op = 'load'), : hook_user() , , , , . , $_SESSION.

+5

Source: https://habr.com/ru/post/1757841/


All Articles