/**
* Drupal 7 Programmatically user Login
*/
function hook_menu(){
$itmes['user/form'] = array(
'title' => t('Example Form'),
'description' => 'Drupal Example Form',
'page callback' => 'drupal_get_form',
'page arguments' => array('example_form'),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
);
return $itmes;
}
function otp_login_form($form, &$form_state){
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#description' => t('Enter your @site_name username.',
array('@site_name'=> variable_get('site_name'))),
'#required' => TRUE,
'#size' => 60,
'#maxlength' => 60,
'#weight' => 2,
);
$form['password'] = array(
'#type' => 'password',
'#title' => t('Password'),
'#description' => t('Enter the password that accompanies your username.'),
'#required' => TRUE,
'#size' => 60,
'#maxlength' => 60,
'#weight' => 3,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Login'),
'#weight' => 4,
);
return $form;
}
function otp_login_form_submit($form, &$form_state){
if(user_authenticate($form_state['values']['name'], $form_state['values']['password'])) {
$user_obj = user_load_by_name($form_state['values']['name']);
$form_state['uid'] = $user_obj->uid;
user_login_submit($form,$form_state);
return true;
}
else {
form_set_error('name', t('Sorry, unrecognized username or password.'));
watchdog('user', 'Login attempt by unregistered user %user.', array('%user' => $form_state['values']['name']));
}
}