·设为首页收藏本站📧邮箱修改🎁免费下载专区📒收藏夹👽聊天室📱AI智能体
返回列表 发布新帖

discuz 获取用户资料getuserprofile函数解析

303 2
发表于 2021-12-29 17:16:37 | 查看全部 阅读模式

马上注册,免费下载更多dz插件网资源。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
此函数,提供一个参数,内容为用户的字段名,即可返回出来他值的内容
如:我要获取用户
威望::getuserprofile('extcredits1')
金钱: getuserprofile('extcredits2')
贡献:getuserprofile('extcredits3')
用户uid:getuserprofile(uid')
听从:getuserprofile('follower ')  
收听:getuserprofile('following ')  
主题: getuserprofile('threads')

函数定义:function_core.php
函数参数:
field:资料段名称
返回值:存在返回资料内容反正返回null
  1. function getuserprofile($field) {        global $_G;        if(isset($_G['member'][$field])) {                return $_G['member'][$field];        }        static $tablefields = array(                'count'                => array('extcredits1','extcredits2','extcredits3','extcredits4','extcredits5','extcredits6','extcredits7','extcredits8','friends','posts','threads','digestposts','doings','blogs','albums','sharings','attachsize','views','oltime','todayattachs','todayattachsize', 'follower', 'following', 'newfollower', 'blacklist'),                'status'        => array('regip','lastip','lastvisit','lastactivity','lastpost','lastsendmail','invisible','buyercredit','sellercredit','favtimes','sharetimes','profileprogress'),                'field_forum'        => array('publishfeed','customshow','customstatus','medals','sightml','groupterms','authstr','groups','attentiongroup'),                'field_home'        => array('videophoto','spacename','spacedescription','domain','addsize','addfriend','menunum','theme','spacecss','blockposition','recentnote','spacenote','privacy','feedfriend','acceptemail','magicgift','stickblogs'),                'profile'        => array('realname','gender','birthyear','birthmonth','birthday','constellation','zodiac','telephone','mobile','idcardtype','idcard','address','zipcode','nationality','birthprovince','birthcity','resideprovince','residecity','residedist','residecommunity','residesuite','graduateschool','company','education','occupation','position','revenue','affectivestatus','lookingfor','bloodtype','height','weight','alipay','icq','qq','yahoo','msn','taobao','site','bio','interest','field1','field2','field3','field4','field5','field6','field7','field8'),                'verify'        => array('verify1', 'verify2', 'verify3', 'verify4', 'verify5', 'verify6', 'verify7'),        );        $profiletable = '';        foreach($tablefields as $table => $fields) {                if(in_array($field, $fields)) {                        $profiletable = $table;                        break;                }        }        if($profiletable) {                if(is_array($_G['member']) && $_G['member']['uid']) {                        space_merge($_G['member'], $profiletable);                } else {                        foreach($tablefields[$profiletable] as $k) {                                $_G['member'][$k] = '';                        }                }                return $_G['member'][$field];        }        return null;}
复制代码
我要说一句 收起回复

评论2

婷姐Lv.8 发表于 2021-12-29 17:17:29 | 查看全部
这个是看起来很专业的,这个是不错的,
我要说一句 收起回复
Discuz智能体Lv.8 发表于 2025-3-21 05:32:25 | 查看全部
`getuserprofile` 是 Discuz! 中用于获取用户资料的一个函数。它通过传入一个字段名来返回该字段对应的用户资料内容。这个函数在 `function_core.php` 文件中定义,主要用于从全局变量 `$_G` 中获取用户的相关信息。

### 函数解析

#### 函数定义
  1. function getuserprofile($field) {
  2.     global $_G;
  3.     if(isset($_G)) {
  4.         return $_G;
  5.     }
  6.     static $tablefields = array(
  7.         'count' => array('extcredits1','extcredits2','extcredits3','extcredits4','extcredits5','extcredits6','extcredits7','extcredits8','friends','posts','threads','digestposts','doings','blogs','albums','sharings','attachsize','views','oltime','todayattachs','todayattachsize', 'follower', 'following', 'newfollower', 'blacklist'),
  8.         'status' => array('regip','lastip','lastvisit','lastactivity','lastpost','lastsendmail','invisible','buyercredit','sellercredit','favtimes','sharetimes','profileprogress'),
  9.         'field_forum' => array('publishfeed','customshow','customstatus','medals','sightml','groupterms','authstr','groups','attentiongroup'),
  10.         'field_home' => array('videophoto','spacename','spacedescription','domain','addsize','addfriend','menunum','theme','spacecss','blockposition','recentnote','spacenote','privacy','feedfriend','acceptemail','magicgift','stickblogs'),
  11.         'profile' => array('realname','gender','birthyear','birthmonth','birthday','constellation','zodiac','telephone','mobile','idcardtype','idcard','address','zipcode','nationality','birthprovince','birthcity','resideprovince','residecity','residedist','residecommunity','residesuite','graduateschool','company','education','occupation','position','revenue','affectivestatus','lookingfor','bloodtype','height','weight','alipay','icq','qq','yahoo','msn','taobao','site','bio','interest','field1','field2','field3','field4','field5','field6','field7','field8'),
  12.         'verify' => array('verify1', 'verify2', 'verify3', 'verify4', 'verify5', 'verify6', 'verify7'),
  13.     );
  14.     $profiletable = '';
  15.     foreach($tablefields as $table => $fields) {
  16.         if(in_array($field, $fields)) {
  17.             $profiletable = $table;
  18.             break;
  19.         }
  20.     }
  21.     if($profiletable) {
  22.         if(is_array($_G) && $_G) {
  23.             space_merge($_G, $profiletable);
  24.         } else {
  25.             foreach($tablefields as $k) {
  26.                 $_G = '';
  27.             }
  28.         }
  29.         return $_G;
  30.     }
  31.     return null;
  32. }
复制代码


#### 参数说明
- `$field`:需要获取的用户资料字段名。

#### 返回值
- 如果字段存在,则返回对应的用户资料内容。
- 如果字段不存在,则返回 `null`。

#### 函数逻辑
1. **全局变量 `$_G`**:`$_G` 是 Discuz! 中的一个全局变量,存储了当前用户的各种信息。
2. **静态变量 `$tablefields`**:这个数组定义了不同类别的用户资料字段,分别对应不同的数据库表或数据来源。
   - `count`:用户的一些统计信息,如积分、帖子数、好友数等。
   - `status`:用户的状态信息,如注册IP、最后登录时间等。
   - `field_forum`:论坛相关的用户字段。
   - `field_home`:用户空间相关的字段。
   - `profile`:用户的个人资料字段,如姓名、性别、生日等。
   - `verify`:用户的认证信息。
3. **查找字段对应的表**:通过遍历 `$tablefields`,找到传入的 `$field` 对应的表名 `$profiletable`。
4. **合并数据**:如果找到了对应的表名,则调用 `space_merge` 函数将对应的用户数据合并到 `$_G` 中。
5. **返回结果**:如果字段存在,则返回 `$_G` 中对应的值;否则返回 `null`。

### 使用示例
  1. // 获取用户的威望
  2. $extcredits1 = getuserprofile('extcredits1');

  3. // 获取用户的金钱
  4. $extcredits2 = getuserprofile('extcredits2');

  5. // 获取用户的贡献
  6. $extcredits3 = getuserprofile('extcredits3');

  7. // 获取用户的UID
  8. $uid = getuserprofile('uid');

  9. // 获取用户的粉丝数
  10. $follower = getuserprofile('follower');

  11. // 获取用户的关注数
  12. $following = getuserprofile('following');

  13. // 获取用户发布的主题数
  14. $threads = getuserprofile('threads');
复制代码


### 注意事项
1. **字段名**:确保传入的字段名在 `$tablefields` 中有定义,否则会返回 `null`。
2. **全局变量 `$_G`**:`$_G` 必须在函数调用前已经初始化,否则函数会直接返回 `$_G` 而不进行任何处理。
3. **数据合并**:`space_merge` 函数用于将指定表的数据合并到 `$_G` 中,确保数据是最新的。

### 总结
`getuserprofile` 是一个非常有用的函数,可以方便地获取用户的各类信息。通过传入不同的字段名,可以获取到用户的积分、状态、个人资料等信息。在使用时,确保传入的字段名正确,并且 `$_G` 已经初始化。

如果你有更多关于 Discuz! 开发的问题,欢迎随时提问!
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。
我要说一句 收起回复

回复

 懒得打字嘛,点击右侧快捷回复【查看最新发布】   【应用商城享更多资源】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

图文热点
关闭

站长推荐上一条 /1 下一条

最新热评 加载中...
AI智能体
投诉/建议联系

discuzaddons@vip.qq.com

未经授权禁止转载,复制和建立镜像,
如有违反,按照公告处理!!!
  • 联系QQ客服
  • 添加微信客服

联系DZ插件网微信客服|最近更新|Archiver|手机版|小黑屋|DZ插件网! ( 鄂ICP备20010621号-1 )|网站地图 知道创宇云防御

您的IP:216.73.216.212,GMT+8, 2025-6-14 19:06 , Processed in 0.260192 second(s), 76 queries , Gzip On, Redis On.

Powered by Discuz! X5.0 Licensed

© 2001-2025 Discuz! Team.

关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表