How can I get the name of the currently logged in user in Drupal 7 in the external file? 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-&gt;name;


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

$account = user_load(2);
print $account-&gt;name;


How can I get the name of the current user in Drupal 7?
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