views:

71

answers:

2

I'm pulling my hair out trying to figure this one out. I can't get the Bootstrap to work properly when outside of the Drupal dir. It works fine if I run this code in the Drupal dir, but up one level doesn't work.

My Drupal path is /public_html/drupal/. The script I'm running is in /public_html.

$user is not returning the logged in user. I've made sure it's not a cross-domain issue (i.e. www.domain.com vs. domain.com).

chdir('/path/to/drupal');

include_once('./includes/bootstrap.inc');
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
global $user;

if ($user->uid) {
    print "Logged in";
} else {
   print "Logged out";
}
+1  A: 

Drupal uses a lot of heavy wizardry to figure out what environment to use: in part because there could be multiple sites in the same Drupal install.

One thing you're missing is $base_url. This needs to be set to the URL to the Drupal site you want to boostrap (e.g. $base_url = "http://example.com").

The other thing you're going to run into, but it looks like you already have it taken care of, is that your script and Drupal need to have the same FQDN. Putting the script on http://foo.example.com and having Drupal live on http://example.com will not work. So, unless Drupal is living on http://example.com/drupal and your script is living on http://example.com, your script will always return the anonymous user object.


Edit

You could have your script in a FQDN that's not the same as Drupal's (like, for example, having a Drupal site at http://drupal.example.com which points to /var/www/drupal and having your script at http://external.example.com/test.php which points to /var/www/test.php; but, in this case, you'd need to have logged in at http://external.example.com/drupal instead of http://drupal.example.com.

Mark Trapp
Thanks! Does the $base_url var go before or after the include?
Ian Silber
You need it before the bootstrap.inc include. Drupal uses it during bootstrap to determine its environment.
Mark Trapp
Hmm - the `$base_url` variable should be defined in your settings.php file, which gets read by the bootstrapping logic, so there should be no need to define/override it in the script.
Henrik Opel
$base_url has to be explicitly defined in settings.php; otherwise, Drupal guesses based on $_SERVER['HTTP_HOST'], which will not be set correctly for a script outside of the Drupal directory. Setting it in the script allows the Drupal installation to remain untouched. This is especially useful when you have a site, http://example.com, in /var/www/drupal, and your script is http://foo.com/script.php which points to /var/www/script.php.
Mark Trapp
Darn - still not working. Tried adding $base_url = 'http://domain.com/drupal'; to both settings.php and the script, still getting the user back as anonymous. Any other ideas?
Ian Silber
Wow - solved by @Henrik Opel's tip! Even though the domain is the exact same.
Ian Silber
+2  A: 

If the user is not recognized by Drupal in your separate script, chances are high that it does not receive the proper session cookie. You should check the cookies set by your normal Drupal install and see if they get send to your script as well. If not, check the $cookie_domain variable in your Drupals settings.php - ususally it is commented out, but you might need to set it explicitly in your case.

Henrik Opel
Thanks! Set the $cookie_domain to "mydomain.com" and it worked like a charm!
Ian Silber