1

I'm trying to check if there is a user logged into drupal in a PHP script that resides within a subdirectory of the drupal root. I know that there can be issues with calling the bootstrap anywhere apart from in the drupal root, so I've created a drupal_bridge.inc.php file inside the root containing the following code, which I then call in the PHP script using include.

<?php 

define("DRUPAL_ROOT",     "/var/www/vhosts/mysite.com/httpdocs/");

$current_wd = getcwd();

chdir(DRUPAL_ROOT);

print getcwd();

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


global $base_url;

$base_url = (array_key_exists('HTTPS', $_SERVER) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'];

drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

chdir($current_wd);

global $user;

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

echo 'INCLUDED';

?>

However, when I visit the PHP script in the browser it says I am logged out. Strangely though if I visit the drupal_bridge.inc.php file directly it can access the user information and it says that I am logged in.

I have set $cookie_domain = .mysite.com in the settings file as well. I have password protected the directory containing the PHP script but I didn't think that made a difference at this level.

Any ideas?

Also, I have looked into turning this in a module but it is not really practical so I would appreciate it if you did not advise doing so.

UPDATE: I just did a print_r($GLOBALS); to see if I could find out what was actually available and I found the following errors, if that helps. http://pastebin.com/YtHPGD75 (too long to post here)

4
  • your bootstrap and "real" Drupal installation are generating two different sessions, perhaps? echo out the sessionid and compare the two.
    – jdu
    Jul 19, 2013 at 17:20
  • @jdu Yes, I can confirm the two session id's are different. Any idea why this may be?
    – Andy
    Jul 19, 2013 at 20:13
  • can you print_r($user) ? I have a feeling you're loading hte Anon user in your boostrap script.
    – jdu
    Jul 22, 2013 at 12:58
  • @jdu I added print_r($user); to the above code and nothing is printed in the PHP script, but if I go to drupal_bride.inc.php I can see the full object for the user that is currently logged in.
    – Andy
    Jul 22, 2013 at 13:11

3 Answers 3

1

The bare minimum required is:

define('DRUPAL_ROOT', '/var/www/vhosts/mysite.com/httpdocs/');
$_SERVER['HTTP_HOST'] = 'mysite.com';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

If you don't want to fully bootstrap and only get the session, see the technique defined in Getting the current user without bootstrapping to DRUPAL_BOOTSTRAP_SESSION

1
  • Thank you for your answer but unfortunately this issue was not due to the bootstrap code. Please see my answer
    – Andy
    Oct 31, 2013 at 20:28
1

After some time, and lots of experimentation, I have a solution at last thanks to a discussion on osTicket. Unfortunately the issue was less due to Drupal, more down to osTicket and the fact that I had included the 'drupal bridge' in the wrong file. However an important note to make is that a drupal bridge - a file residing inside the Drupal root directory including bootstrap code - must be used to access session information in external scripts, rather than using the bootstrap code directly.

I hope that somebody will find this useful.

1
  • Your idea of a bridge/proxy script is clever.
    – MV.
    Feb 24, 2014 at 22:42
0

setting the $base_url in settings.php rather than leave Drupal to resolve it will solve the problem.

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.