php 检查用户是否已经存在于drupal中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2803425/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:42:49  来源:igfitidea点击:

Checking whether a user already exists in drupal

phpdrupaldrupal-fapidrupal-alter

提问by simonr

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 ?

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

Some sample code would be really appreciated. Please help.

一些示例代码将不胜感激。请帮忙。

Thank You.

谢谢你。

回答by Mika Andrianarijaona

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

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

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

回答by googletorp

This can be done with hook_form_alter:

这可以通过以下方式完成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_loadas it hooks into other modules as well.

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

回答by Arla

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

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

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

回答by Jasom Dotnet

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

您可以尝试查看以下 2 个模块以获取灵感:friendly_registerusername_check

回答by dawoodman71

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

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

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

Hope this helps someone else.

希望这对其他人有帮助。