Hi everyone, I'm new to Drupal.
I'm trying to check if an user is logged in Drupal from a test.php empty file (in the same domain and folder of drupal index.php).

Is possible to do this?
Thankyou in advance.

Comments

bboldi’s picture

Have not tried it, just wrote from scratch:

// define static var
define('DRUPAL_ROOT', getcwd());

// include bootstrap
include_once('./includes/bootstrap.inc');

// initialize stuff
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

//laod user
global $user;

if($user->uid >0)
{
    // user logged in
    echo 'Welcome '.$user->name.'!';
}
else
{
    // anonym user
    echo 'Welcome guest!';    
}

Hope it works ;)

qwerty_123’s picture

Thankyou!
It works fine.
In this way i can use all Drupal function in my external file? And get all drupal propertyes and data?
I think yes...

bboldi’s picture

Well you should be able to access all the globally available functions and variables, because the _drupal_bootstrap_full() in drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); calls the module_invoke_all() function http://api.drupal.org/api/drupal/includes--module.inc/function/module_in....

qwerty_123’s picture

Thankyou very much.
I have only one problem. When i click on the logout link, i get "Page not found".
Where is the problem?
The link is: http://localhost/mysite/?q=it/logout
Thankyou

dman’s picture

I know the short documentation for this existed here at some time, but today, the best short example of using drupal_bootstrap() seems to be at avenuewebmedia

Minimal required 2 lines are : http://drupal.org/node/579128#comment-2048732

bboldi’s picture

That's true for Drupal 5.x and 6.x but for 7.x you need the first line as well

// define static var
define('DRUPAL_ROOT', getcwd()); 
qwerty_123’s picture

Thankyou i've solved. The problem was the right link is "user/logout"