php drupal 7 以编程方式登录用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8078266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
drupal 7 programmatically log user in
提问by Felix Eve
After having just registered a new account and created a profile how would I log a user in?
刚刚注册了一个新帐户并创建了个人资料后,我将如何登录用户?
I have tried :
我试过了 :
global $user;
$user = user_load($account->uid);
or
或者
global $user;
$user = user_load(array('mail' => $_POST['email'], 'pass' => trim($_POST['password'])));
but neither work and the second results in an error about array_flip.
但两者都不起作用,第二个导致关于 array_flip 的错误。
回答by Ken
I'm going to answer this for future reference, because the third answer above is wrong, and the first answer will work but is unnecessary (it replicates the experience of the user submitting the login form, calling all validators etc, and presumably you have already done that validation or you wouldn't be trying to log the user in directly.
我将回答这个以供将来参考,因为上面的第三个答案是错误的,第一个答案会起作用但没有必要(它复制了用户提交登录表单、调用所有验证器等的体验,并且大概你有已经完成该验证,否则您将不会尝试直接登录用户。
This will work as expected, assuming you have $username
and $password
from your own form or function, and you know the user is not logged in:
这将如预期,假设你有$username
,并$password
从自己的外形还是功能,并且您知道用户没有登录:
if ($uid = user_authenticate($username, $password)) {
global $user;
$user = user_load($uid);
$login_array = array ('name' => $username);
user_login_finalize($login_array);
}
First you validate the username and password you have. If you get back a non-zero UID, the authentication succeeded. You create an array that provides the one possibly necessary piece of information that was in the original login form, and pass it to user_login_finalize()
, which does all the rest (not just regenerating the session, but also recording the login properly, and calling login hooks).
首先,您验证您拥有的用户名和密码。如果您返回非零 UID,则身份验证成功。您创建一个数组,该数组提供原始登录表单中可能需要的一条信息,并将其传递给user_login_finalize()
,它完成所有其余工作(不仅重新生成会话,而且还正确记录登录,并调用登录挂钩) .
回答by Clive
Drupal does it using user_login_finalize
from user_login_submit
, you can invoke the same thing yourself with this code:
Drupal 使用user_login_finalize
from 来完成它user_login_submit
,您可以使用以下代码自己调用相同的内容:
$form_state['uid'] = $account->uid;
user_login_submit(array(), $form_state);
回答by anoopjohn
You can login programmatically in D7 using the following code.
您可以使用以下代码在 D7 中以编程方式登录。
global $user;
$user = user_load($uid);
drupal_session_regenerate();
That should log in the user with the given user id.
那应该使用给定的用户 ID 登录用户。
回答by Jim
Use the following code to Create programmatically a user account in Drupal 8.
使用以下代码在 Drupal 8 中以编程方式创建用户帐户。
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$user = \Drupal\user\Entity\User::create();
//Mandatory settings
$user->setPassword('password');
$user->enforceIsNew();
$user->setEmail('email');
$user->setUsername('user_name');//This username must be unique and accept only a-Z,0-9, - _ @ .
//Optional settings
$user->set("init", 'email');
$user->set("langcode", $language);
$user->set("preferred_langcode", $language);
$user->set("preferred_admin_langcode", $language);
//$user->set("setting_name", 'setting_value');
$user->activate();
//Save user
$res = $user->save();
回答by Radheshyam Kumawat
/**
* 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']));
}
}