Drupal 6 programmatically logs the user into

I am trying to login as part of the submit form, but why the following does not work:

$user = db_fetch_object(db_query("SELECT * FROM users WHERE mail = '%s' AND pass = '%s'", $mail, md5($pass)));

if ($user) {
    // Authenticate user and log in
    $params = array(
      'name' => $user->name,
      'pass' => trim($user->pass)
    );

    // Authenticate user    
    $account = user_authenticate($params);
}

if I unload $ user, I can see the correct values, but if I reset the account, it will be empty.

+3
source share
1 answer

You pass the hashed password to 'user_authenticate ()', while the function expects to clear the password (it will use hash indirectly when loading the account through 'user_load ()' ).

So, changing the declaration of the $ params array to

$params = array(
  'name' => $user->name,
  'pass' => $pass
);

should do your work example.


By the way, you could use it user_load()yourself so as not to directly query the database:

$user = user_load(array('mail' => $mail, 'pass' => trim($pass), 'status' => 1));

('status' => 1 - , , ;)

+4

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


All Articles