【问题标题】:Drupal: getting user informationDrupal:获取用户信息
【发布时间】:2011-03-13 18:09:44
【问题描述】:

当我加载随机网站页面时,如何获取有关登录用户的信息?

我需要获取个人资料字段来自定义页面外观

谢谢

【问题讨论】:

  • 您是在编写自定义模块,还是尝试使用前端?请提供有关您要实现的目标的更多信息。

标签: drupal drupal-6 profile


【解决方案1】:

以下是您可以使用的一系列变量的列表:http://web.rhizom.nl/development/drupal-variables-in-theme-files/

此页面显示如何获取变量,例如用户电子邮件地址:http://11heavens.com/Drupal-coder-lost-in-space/who-is-she

【讨论】:

    【解决方案2】:

    Erics 的回答适用于主题。模块中的标准方法是使用global $user 来获取登录用户。我个人不喜欢以这种方式使用全局变量,但是在罗马时...

    【讨论】:

      【解决方案3】:

      当前用户信息始终作为全局可用,因此您只需:

      global $user;
      // $user will now be a stdClass object representing the current user, logged in or not
      // If you are only interested in logged in users, a standard check would be
      if (0 != $user->uid) {
        // Do something for/with logged in users
      }
      

      匿名用户的 uid 为 0,管理员(第一个)用户的 uid 为 1。

      【讨论】:

        【解决方案4】:

        配置文件值始终加载到使用函数user_load() 获得的任何用户对象中。全局变量$user 包含当前登录用户的信息。如果要查找名为profile_realname 的配置文件的值,可以使用以下代码:

        global $user:
        
        // Avoid the anonymous user, which doesn't have any profile field.
        if ($user->uid) {
          $realname = $user->profile_realname;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-06-30
          • 2011-02-23
          • 1970-01-01
          • 1970-01-01
          • 2015-07-29
          • 2018-03-26
          • 2020-04-21
          • 2018-10-28
          相关资源
          最近更新 更多