手动登录用户

发布于 2024-09-16 06:03:06 字数 745 浏览 1 评论 0原文

我正在开发一个 drupal 网站,允许用户在登录的同时发布内容。我已成功添加电子邮件和密码字段更改为原始表单,但我不知道应该如何实际登录用户。 (我的计划是在创建内容之前的验证步骤中执行此操作,以使登录用户成为该内容的所有者)。

我可以在 user.module API 中找到三个看起来正确的函数:

现在,我的问题是它是哪一个?我是否走在正确的轨道上?

I'm working on a drupal site where I allow users to login while at the same time posting a content. I've successfully added email & password fields to the original form, but I'm stuck as to how I should actually log in the user. (My plan is to do it in the validation step, before the content is created, to make the logged in user owner to the content).

I can find three functions in user.module API that somehow looks right:

Now, my question is which one is it? Am I even on the right track?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方觉久 2024-09-23 06:03:06

一旦您检查了用户名/密码并发现它们验证并获得了用户的 $uid ,您将执行如下操作:

$account = user_load($uid))
global $user;
$user = $account;
user_authenticate_finalize($form_state['values']);

因此您覆盖全局 $user code> 对象并调用 user_authenticate_finalize

更新:
一步完成验证和登录将如下所示:

$account = user_load(array(
  'name' => $form_values['name'],
  'pass' => trim($form_values['pass']),
  'status' => 1)
);
if ($account && !drupal_is_denied('mail', $account->mail) {
  global $user;
  $user = $account;
  user_authenticate_finalize($form_state['values']);
}
else {
  // Raise validation error.
}

Once you've checked the username/password and have found that they validate and got the $uid of the user, you would do something like this:

$account = user_load($uid))
global $user;
$user = $account;
user_authenticate_finalize($form_state['values']);

So you overwrite the global $user object and call user_authenticate_finalize.

Update:
Doing the validation and login with one step would look like this:

$account = user_load(array(
  'name' => $form_values['name'],
  'pass' => trim($form_values['pass']),
  'status' => 1)
);
if ($account && !drupal_is_denied('mail', $account->mail) {
  global $user;
  $user = $account;
  user_authenticate_finalize($form_state['values']);
}
else {
  // Raise validation error.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文