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)
print_r($user);
to the above code and nothing is printed in the PHP script, but if I go todrupal_bride.inc.php
I can see the full object for the user that is currently logged in.