I want to get the uid of a logged in user in an external php script. I am using:

require_once './includes/bootstrap.inc';

$account1 = user_uid_optional_load();
echo $account1;

But it always return :

stdClass Object ( [uid] => 0 [name] => [pass] => [mail] => [mode] => 0 [sort] => 0 [threshold] => 0 [theme] => [signature] => [signature_format] => 0 [created] => 0 [access] => 0 [login] => 0 [status] => 0 [timezone] => [language] => [picture] => [init] => [data] => [roles] => Array ( [1] => anonymous user ) )

irrespective of admin, authenticated or anonymous user. I also tried these but the result remains same.

global $user; 
echo $user;

$account1 = user_load(array('name' => check_plain($name))); echo $account1;

I also checked a similar problem and uncommented $cookie_domain = ''; ( blank cookie domain for localhost )in my settings.php but to no avail.
How can I get some non-zero uid for logged in users ?

1 Answer 1


Because you need to login from external script... Your external script is always anonymous to Drupal since no session has been created for external script user....

If you are trying to load all online users in an external script you can check who's online block implementation in user module..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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