user_authenticate

5 user.module user_authenticate($name, $pass)
6 user.module user_authenticate($form_values = array())
7 user.module user_authenticate($name, $password)
8 user.module user_authenticate($name, $password)

Try to log in the user locally.

Parameters

$form_values: Form values with at least 'name' and 'pass' keys, as well as anything else which should be passed along to hook_user op 'login'.

Return value

A $user object, if successful.

4 calls to user_authenticate()

File

modules/user/user.module, line 1371
Enables the user registration and login system.

Code

function user_authenticate($form_values = array()) {
  global $user;

  // Load the account to check if the e-mail is denied by an access rule.
  // Doing this check here saves us a user_load() in user_login_name_validate()
  // and introduces less code change for a security fix.
  $account = user_load(array('name' => $form_values['name'], 'pass' => trim($form_values['pass']), 'status' => 1));
  if ($account && drupal_is_denied('mail', $account->mail)) {
    form_set_error('name', t('The name %name is registered using a reserved e-mail address and therefore could not be logged in.', array('%name' => $account->name)));
  }

  // Name and pass keys are required.
  // The user is about to be logged in, so make sure no error was previously
  // encountered in the validation process.
  if (!form_get_errors() && !empty($form_values['name']) && !empty($form_values['pass']) && $account) {
    $user = $account;
    user_authenticate_finalize($form_values);
    return $user;
  }
  else {
    watchdog('user', 'Login attempt failed for %user.', array('%user' => $form_values['name']));
  }
}

Comments

Register user and log in

A simple example to programatically register a new user and log them in:

<?php
// Set basic details for the new user.
$params = array(
 
'mail'   => 'foo@example.com',
 
'name'   => 'foo',
 
'pass'   => 'bar', // No need to hash the password
 
'status' => 1, // Otherwise the user is blocked on creation
);

// Create the account. The first parameter is not needed unless updating an existing account.
$account = user_save(NULL, $params);

// Log the user in. $params array must have a minimum of 'name' and 'pass' keys.
$account = user_authenticate($params);
?>

I use the user_authenticate

I use the user_authenticate and it works because the $account var contains uid and some stuff, but I can't see the session cookie

Lets say i have drupal in:

http://localhost/drupal6

and the user_authenticate is used on:

http://localhost/drupal6/some.php

when i go to the first url, the logged user is gone

Login or register to post comments