dongyuan1870 2015-04-17 12:11
浏览 75

Drupal 7:全局$ user无效

I am working on a Drupal module,trying to get uname and uid of the global variable $user: if $user is used within .module file it works but if not I get : Trying to get property of non-object in ... on line 377 and var_dump($user) gives null , here is my code :

MyModuleAPI.php:

function get_DB_Handler($projectname)
{  
    global $user;
    $u_name = $user->name;
    $u_id = $user->uid; //line 377
...
}

this function is called after an ajax POST query.

I will be grateful if someone clarify for me where is the difference since all files are in the same module. Any Help will be appreciated.

  • 写回答

1条回答 默认 最新

  • dqbhdsec59405 2015-04-17 12:19
    关注

    $user global variable is defined when Drupal bootstraps. You must call appropriate functions from the core to make it visible inside non-module code. Put the following to the beginning of you file:

    <?php
    $file_path = dirname(__FILE__);
    $path = preg_replace('/sites.*/', "", $file_path);
    //adjust the path to your website root directory
    $path = substr_replace($path ,"",-1);
    
    $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
    $_SERVER['HTTP_HOST'] = 'site_hostname';
    
    require_once($path . DIRECTORY_SEPARATOR . 'includes' .  DIRECTORY_SEPARATOR . 'bootstrap.inc');
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程