1

Consider the following script.

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

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

$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);
drupal_session_regenerate();
?>

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?

0

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:

<?php
chdir('../');
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
?>

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 /.

| improve this answer | |
  • 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. – Silox Jun 25 '13 at 7:25
  • Drupal 7 uses the DRUPAL_ROOT constant. If it's not defined, it will throw warnings. – kiamlaluno Feb 25 '19 at 6:35

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.