user_login
Definition
user_login($edit = array(), $msg = '')
modules/user.module, line 802
Code
<?php
function user_login($edit = array(), $msg = '') {
global $user, $base_url;
// If we are already logged on, go to the user page instead.
if ($user->uid) {
drupal_goto('user');
}
if (user_deny('user', $edit['name'])) {
$error = t('The name %s has been denied access.', array('%s' => theme('placeholder', $edit['name'])));
}
else if ($edit['name'] && $edit['pass']) {
if (!$user->uid) {
$user = user_authenticate($edit['name'], trim($edit['pass']));
}
if ($user->uid) {
watchdog('user', t('Session opened for %name.', array('%name' => theme('placeholder', $user->name))));
// Update the user table timestamp noting user has logged in.
db_query("UPDATE {users} SET changed = '%d' WHERE uid = '%s'", time(), $user->uid);
user_module_invoke('login', $edit, $user);
$old_session_id = session_id();
session_regenerate_id();
db_query("UPDATE {sessions} SET sid = '%s' WHERE sid = '%s'", session_id(), $old_session_id);
// Redirect the user to the page he logged on from.
drupal_goto();
}
else {
if (!$error) {
$error = t('Sorry. Unrecognized username or password.') .' '. l(t('Have you forgotten your password?'), 'user/password');
}
watchdog('user', t('Login attempt failed for %user: %error.', array('%user' => theme('placeholder', $edit['name']), '%error' => theme('placeholder', $error))));
}
}
// Display error message (if any):
if ($error) {
drupal_set_message($error, 'error');
}
// Display login form:
if ($msg) {
$output .= '<p>'. check_plain($msg) .'</p>';
}
if (count(user_auth_help_links()) > 0) {
$output .= form_textfield(t('Username'), 'name', $edit['name'], 30, 64, t('Enter your %s username, or an ID from one of our affiliates: %a.', array('%s' => variable_get('site_name', 'local'), '%a' => implode(', ', user_auth_help_links()))));
}
else {
$output .= form_textfield(t('Username'), 'name', $edit['name'], 30, 64, t('Enter your %s username.', array('%s' => variable_get('site_name', 'local'))));
}
$output .= form_password(t('Password'), 'pass', $pass, 30, 64, t('Enter the password that accompanies your username.'));
$output .= form_submit(t('Log in'));
return form($output, 'post', url('user/login', drupal_get_destination()));
}
?> 