Consider the following script.

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

require_once DRUPAL_ROOT . '/includes/bootstrap.inc';

$username = "Silox";
$password = "hidden";

global $user;

$account = user_authenticate($username, $password);

// Woah, it seems like we don't know this user, let's create him!
if (!$account) {
    $userinfo = array(
        'name' => $username,
        'pass' => $password,
        'init' => $username,
        'status' => 1,
        'access' => REQUEST_TIME,
    $account = user_save(drupal_anonymous_user(), $userinfo);
    $account = user_authenticate($username, $password);

$user = user_load($account, TRUE);

This script logs me in or creates a new account when the username and password combination doesn't exist. When I put this script in the base directory of my Drupal installation, everything works perfectly fine.

However, when I put it in the root directory of the PhpBB subdirectory and change the DRUPAL_ROOT to getcwd() . "/.." to compensate for the extra folder, it doesn't work. If I print out my user array at the end of the function, I get a nice array with corresponding correct info but however, if I return to my Drupal site, I am not logged in.

How can this problem get solved?


1 Answer 1


I think this is related to how you're defining the DRUPAL_ROOT constant.

Several times I've had to make scripts interact with Drupal from sub folders, and I've found the simplest way to make sure things work correctly is to preface my code with the following:

require_once './includes/bootstrap.inc';

The key thing here is the chdir() call.

Also, when creating a path and you want to go up one folder, its usually ../ not /.., because directory paths should usually end with a /.

  • Thank you for your solution but sadly, after I added the chdir and removed the /.. it didn't work. The /.. is actually right since getcwd() leaves out the trailing slash, so I have to compensate for it. The ./ of the require takes care ot the trailing slash. Commented Jun 25, 2013 at 7:25
  • Drupal 7 uses the DRUPAL_ROOT constant. If it's not defined, it will throw warnings.
    – apaderno
    Commented Feb 25, 2019 at 6:35

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.