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?