2

In an external file I use the following code:

$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;

The code doesn't print anything, but this code works:

$account = user_load(2);
print $account->name;

How can I get the name of the current user in Drupal 7?

5
  • 3
    I don't know your situation or use case, but it sounds like you're hacking your way around Drupal instead of fully taking advantage of it. Why are you using an external script instead of writing a custom Drupal module? Jul 22, 2011 at 14:02
  • I need external php file for AJAX. I want reload only part of the page. xmlhttp.open("GET","localhost/drupal/sites/all/themes/MyBartik/templates**team.php***?team_id="+str,true);
    – Alexey
    Jul 22, 2011 at 14:10
  • 1
    You can return only part of a page using the ajax framework. . Jul 22, 2011 at 14:18
  • I already wrote the whole script and I don't have time to understand how this framework works.
    – Alexey
    Jul 22, 2011 at 14:20
  • 3
    @alexey if you don't want to use the framework, you should still create this as a module with a callback instead of a php file.
    – googletorp
    Jul 22, 2011 at 14:29

4 Answers 4

3

drupal_session_initialize() is run as part of the full bootstrap. This will give you a $user object.

If you are not getting a user object that would be a good place to start looking. I would suggest that something with your setup is not quite right.

But I would re-iterate the suggestions that creating a module and learning the framework is the way to go.

3

I advice you to:

  • create a module
  • put your script in that module (inside one or more functions)
  • implement hook_menu() to define the url you can call (eg. example.com/mytuff/ajax) and set your script's (main) function as the 'page callback'
  • instead of returning stuff (like page callbacks normally do) print your output and exit() at the end
1

If there are no sessions open for any authenticated user, then the current user is the anonymous user; in that case, print $user->name doesn't print anything because $user->name is not set.

This is evident in the drupal_anonymous_user() code:

function drupal_anonymous_user() {
  $user = new stdClass();
  $user->uid = 0;
  $user->hostname = ip_address();
  $user->roles = array();
  $user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';
  $user->cache = 0;
  return $user;
}

I agree with the other users that using an external file in this case is not necessary; to understand when an external file is used instead of a module, see Are there cases where a third-party module would need to use its own file similar to xmlrp.php, cron.php, or authenticate.php?

1

In Drupal 6 I found this issue to be related to how the session was being saved. By default the session was saved to the path /. I was able to fix this by uncommenting and configuring the $base_url variable in the settings.php file. My exact issue was that the external file was not in the root of the drupal install and was in a subfolder like www.example.com/mycustomcode/test.php. Therefore the sessions were not set for that path. If you move your code to the root of the install you may find that it works just fine when you call bootstrap full, global $user and print_r $user. I assume this fix may also apply to Drupal 7 but I have not needed this functionality as of yet.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.