检查drupal中是否已存在用户

发布于 2024-08-31 14:17:46 字数 165 浏览 7 评论 0原文

当用户输入他的登录信息并点击提交时,我想检查该用户是否已经存在。 所以,我有以下两个问题 1. 对于用户点击登录表单上的提交按钮的情况,需要实现哪个钩子。我需要用户输入的用户名。 2. 如何以编程方式检查用户是否已存在于 drupal 中?

一些示例代码将非常感激。 请帮忙。

谢谢。

When a user enters his login information and hits submit, i want to check if the user already exists or not.
So, i have the following two questions
1. Which hook is needed to be implemented , for the case when user hits the submit button on the login form. I need the username entered by the user.
2. How to check if a user already exists in drupal or not programmatically ?

Some sample code would be really appreciated.
Please help.

Thank You.

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

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

发布评论

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

评论(5

雨落□心尘 2024-09-07 14:17:46

Drupal 7 提供了一个按名称获取用户对象的函数:

$user = user_load_by_name($name);
if(!$user){
    // User doesn't exist
} 
else {
    // User exists
}

http:// /api.drupal.org/api/drupal/modules%21user%21user.module/function/user_load_by_name/7

Drupal 7 provides a function to get a user object by name :

$user = user_load_by_name($name);
if(!$user){
    // User doesn't exist
} 
else {
    // User exists
}

http://api.drupal.org/api/drupal/modules%21user%21user.module/function/user_load_by_name/7

转身泪倾城 2024-09-07 14:17:46

这可以通过 hook_form_alter 来完成:

function module_(&$form, &$form_state, $form_id) {
  $user_login_forms = array('user_login', 'user_login_block');
  if (in_array($form_id, $user_login_forms)) {
    $form['#validate'][] = 'my_validate_function';
  }
}

function my_validate_function(&$form, &$form_state) {
  $name = $form_state['values']['name'];
  // Drupal 6:
  if (!db_result(db_query("SELECT COUNT(*) FROM {users} WHERE name = '%s';", $name))) {
    // User doesn't exist
  }
  // Drupal 7:
  if (!db_query("SELECT COUNT(*) FROM {users} WHERE name = :name;", array(':name' => $name))->fetchField()) {
    // User doesn't exist
   }
}

在这种情况下,直接查询数据库比使用 user_load 更好,因为它也挂钩到其他模块。

This can be done with hook_form_alter:

function module_(&$form, &$form_state, $form_id) {
  $user_login_forms = array('user_login', 'user_login_block');
  if (in_array($form_id, $user_login_forms)) {
    $form['#validate'][] = 'my_validate_function';
  }
}

function my_validate_function(&$form, &$form_state) {
  $name = $form_state['values']['name'];
  // Drupal 6:
  if (!db_result(db_query("SELECT COUNT(*) FROM {users} WHERE name = '%s';", $name))) {
    // User doesn't exist
  }
  // Drupal 7:
  if (!db_query("SELECT COUNT(*) FROM {users} WHERE name = :name;", array(':name' => $name))->fetchField()) {
    // User doesn't exist
   }
}

It's better to query the DB directly in this case than than using user_load as it hooks into other modules as well.

你怎么这么可爱啊 2024-09-07 14:17:46

在 Drupal 7 中,在验证函数中替换为:

if (!db_query("SELECT COUNT(*) FROM {users} WHERE name = :name", array(':name' => $name))->fetchField()) {
  // User doesn't exist
}

In Drupal 7, substitute for this in the validation function:

if (!db_query("SELECT COUNT(*) FROM {users} WHERE name = :name", array(':name' => $name))->fetchField()) {
  // User doesn't exist
}
岁吢 2024-09-07 14:17:46

我意识到这已经快两年了,但 user_authenticate 做得很好。

$existing_user = user_authenticate($name,$password);
if($existing_user)
  // user exists
else
  // user doesn't exist

希望这对其他人有帮助。

I realize this is almost 2 years old, but user_authenticate does this nicely.

$existing_user = user_authenticate($name,$password);
if($existing_user)
  // user exists
else
  // user doesn't exist

Hope this helps someone else.

反差帅 2024-09-07 14:17:46

您可以尝试查看这两个模块以获取灵感:Friendly_registerusername_check

You can try to look on these 2 modules for inspiration: friendly_register and username_check.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文