Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我能';无法从Drupal 7中的外部文件访问对象全局$user_Php_Drupal_Drupal 7_Global - Fatal编程技术网

Php 我能';无法从Drupal 7中的外部文件访问对象全局$user

Php 我能';无法从Drupal 7中的外部文件访问对象全局$user,php,drupal,drupal-7,global,Php,Drupal,Drupal 7,Global,在我使用的外部文件中: $path = $_SERVER['DOCUMENT_ROOT']; chdir($path."/drupal"); define('DRUPAL_ROOT', getcwd()); require_once './includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); global $user; print $user->name;//it returns nothing 我使用AJA

在我使用的外部文件中:

$path = $_SERVER['DOCUMENT_ROOT'];
chdir($path."/drupal");
define('DRUPAL_ROOT', getcwd());
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

global $user;
print $user->name;//it returns nothing
我使用AJAX加载这个外部文件。可能是因为它我有这些麻烦

xmlhttp.open("GET","localhost/drupal/sites/all/themes/MyBartik/templates**team.php***?team_id="+str,true); 
但这个代码是有效的:

$account = user_load(2);
print $account->name;
如何在Drupal 7中获取当前用户的名称

当我写作时:

print_r $user; 
它返回:

stdClass Object ( [uid] => 0 [hostname] => ::1 [roles] => Array ( [1] => anonymous user ) [cache] => 0 ) 
而不是:

stdClass Object ( [uid] => 1 [name] => admin [pass] => $S$CyyoOFpUD2X.4w8PHsIpWVHinwFZGIG3ZS/uQCz8W/lollB7UwCT [mail] => foo@example.com... 
这里是AJAX函数:

function showTeam(str){                                     
                                             jQuery("#popup_content").fadeIn("slow");                                             
                                            if (str=="")
                                              {
                                              document.getElementById("jShowTeam").innerHTML="";
                                              return;
                                              }
                                            if (window.XMLHttpRequest)
                                              {// code for IE7+, Firefox, Chrome, Opera, Safari
                                              xmlhttp=new XMLHttpRequest();                                       
                                              }
                                            else
                                              {// code for IE6, IE5
                                              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");                                 
                                              }
                                            xmlhttp.onreadystatechange=function()
                                              {
                                              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                                                {
                                                document.getElementById("jShowTeam").innerHTML=xmlhttp.responseText;
                                                }
                                              }

                                            xmlhttp.open("GET","http://localhost/drupal/sites/all/themes/MyBartik/templates/team.php?team_id="+str,true);                           xmlhttp.send();                                     
}

只是有同样的问题(使用完全相同的代码)。我相信OP在过去的两年里继续前进,但我会把这个留给你——是的,读者

打开此文件:

sites/default/settings.php
在328号线上有一条逗号线,如

#$cookie_domain = '.example.com';

取消对它的注释并将您的域放在那里,不要忘记在前缀点中留下。您必须再次登录,它应该可以正常工作。

刚刚遇到同样的问题(使用完全相同的代码)。我相信OP在过去的两年里继续前进,但我会把这个留给你——是的,读者

打开此文件:

sites/default/settings.php
在328号线上有一条逗号线,如

#$cookie_domain = '.example.com';

取消对它的注释并将您的域放在那里,不要忘记在前缀点中留下。您必须重新登录,它应该可以工作。

外部文件在哪里?同一个域名?在同一个域中,您从何处调用AJAX?这可能有助于得到一些答案。外部php放在这里:
C:\xamplite\htdocs\drupal\sites\all\themes\MyBartik\templates\team.php
我从放在这里的文件调用AJAX:
C:\xamplite\htdocs\drupal\sites\all\themes\MyBartik\js\showteam.js
我没有时间检查,但它可能会工作:如果你正在编写自己的模块中,在调用需要完全加载的$user对象的函数之前,始终调用user_load(),除非您确定已发生这种情况。外部文件在哪里?同一个域名?在同一个域中,您从何处调用AJAX?这可能有助于得到一些答案。外部php放在这里:
C:\xamplite\htdocs\drupal\sites\all\themes\MyBartik\templates\team.php
我从放在这里的文件调用AJAX:
C:\xamplite\htdocs\drupal\sites\all\themes\MyBartik\js\showteam.js
我没有时间检查,但它可能会工作:如果你正在编写自己的模块中,在调用需要完全加载的$user对象的函数之前,始终调用user_load(),除非您确定这已经发生。