1

I have a stand-alone php script with the following lines at the beginning:

define('DRUPAL_ROOT', '../../../../../');

require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);                

And this is giving me access to all of Drupal's functions just fine, however, when I try to access the global user object, it's as if the user is not logged in (IE the user object says the user is anonymous and has a UID of 0), even though I know they are because when I browse the site normally, the user is logged in.

Why does it not pick up the user's session in my script?

  • Just a guess, but try using an absolute path to DRUPAL_ROOT, not relative. – David Thomas Jun 21 '13 at 7:54
3

The solution was a one line fix:

define('DRUPAL_ROOT', '../../../../../');
$base_url = 'http://' . $_SERVER['HTTP_HOST'];
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); 

Note the addition of $base_url

|improve this answer|||||
0

Add global $user after drupal_bootstrap, that makes user's $user object available in your script.

Example:

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); 
global $user;
print $user->name;
|improve this answer|||||
0

You need to set $base_url to $_SERVER['HTTP_HOST'] with 'http://' prepended and then it will work from a subdirectory.

|improve this answer|||||
0

This is my boilerplate for stand alone php. I mainly use it for debugging so adjust accordingly.

if (isset($_SERVER['argc']) || isset($_SERVER['argv'])) {
  // Set server variables if cli.
  $_SERVER['REMOTE_ADDR']     = '127.0.0.1';
  $_SERVER['SERVER_SOFTWARE'] = NULL;
  $_SERVER['REQUEST_METHOD']  = 'GET';
  $_SERVER['QUERY_STRING']    = '';
  $_SERVER['HTTP_USER_AGENT'] = 'console';
}

// Find index.php
$webroot = str_replace('\\', '/', dirname(__FILE__));
$webroot = str_replace('\\', '/', realpath(dirname($_SERVER['SCRIPT_FILENAME'])));
while (!empty($webroot)) {
  if (file_exists($webroot . '/index.php') && strpos(file_get_contents($webroot . '/index.php'), 'menu_execute_active_handler();') !== FALSE) {
    break;
  }
  $new_webroot = str_replace('\\', '/', dirname($webroot));
  if ($new_webroot == $webroot) {
    $webroot = str_replace('\\', '/', getcwd());
    break;
  }
  $webroot = $new_webroot;
}
chdir($webroot);
/**
  * Root directory of Drupal installation.
  */
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';

// Set the base URL.
$base_url = 'http://' . $_SERVER['HTTP_HOST'];
// Set https to on if it should be.
if ( (isset($_SERVER["HTTPS"]) && strtolower($_SERVER["HTTPS"]) == "on")
  || (isset($_SERVER["HTTP_X_FORWARDED_PROTO"]) && $_SERVER["HTTP_X_FORWARDED_PROTO"] == "https")
  || (isset($_SERVER["HTTP_HTTPS"]) && $_SERVER["HTTP_HTTPS"] == "on")
    ) {
  $_SERVER["HTTPS"] = "on";
}
// Fix base URL if HTTPS.
if (isset($_SERVER["HTTPS"]) && strtolower($_SERVER["HTTPS"]) == "on" && isset($base_url)) {
  $base_url = str_replace("http://", "https://", $base_url);
}

try {
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  ini_set('memory_limit', '8000M');
  $output = array();
  $time_start = microtime(true);
  set_time_limit(0);
  // header('Content-Type: text/plain');



  // Run code here.



  $time_end = microtime(true);
  $time = round($time_end - $time_start, 4);
  echo "took $time seconds<br>\n";
}
catch (Exception $e) {
  echo 'Caught exception: ',  $e->getMessage(), "\n";
}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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