I'm trying to log in a user as part of a form submit, but why is the following not working:

$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 dump $user I can see the correct values, but if I dump the $account it's empty.

What am I doing wrong?

Comments

kenuck’s picture

Heya,

The $user->pass is the md5 encrypted password. You need to pass the non md5 version.


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

cheers

crossmedia’s picture

Hi, i tested and verified on drupal 6 and below code was working fine.

//$xyz = your UID
$user1 = db_fetch_object(db_query("SELECT * FROM users WHERE uid = $xyz"));
				 if ($user1) {
					$account = user_load(array(
					  'name' => $user1->name,
					  'pass1' => $user1->pass,
					  'mail' => $user1->mail,
					  'status' => 1)
					);
					if ($account){
					  global $user;
					  $user = $account;
					  
					      $user = user_load($xyz); 
				                    }
					else {
					  // Raise validation error.
					drupal_set_message(t('Not validated'.$xyz));
					 
					}