2
votes

I have read this post.

Drupal is installed in my local machine and works fine. I have created a static page inside a directory in the drupal installation. Like http://localhost/usertest/userts.php

If the page moved to root directory it works but when I move the page to usertest or any other directory it is not working.

Directory structure :
root - drupal installed directory containing all the drupal files including includes, modules, sites etc.
usertest - sub directory of root.

Code :
What my userts.php page contains is :

<?php 
$currdir=getcwd();
chdir($_SERVER['DOCUMENT_ROOT']);
require_once("./includes/bootstrap.inc");
drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
global $user;
chdir($currdir);


?>
<p>USER VAR :</p>
<?php print_r( $user ); ?>

OUTPUT : The out put is given as anonymous user though I logged in :

USER VAR :
stdClass Object ( [uid] => 0 [hostname] => 127.0.0.1 [roles] => Array ( [1] => anonymous user ) [session] => [cache] => 0 ) 

But if I remove chdir part and move the file to root directory and access it like http://localhost/userts.php then it gives me the current user object output.

I could not figure out what I have missed here. Can anyone help on this ?

1
@QuickSilver My requirement is to get only the current user to a page. And do some customization which is outside the Drupal. So a module is heavy for my requirement. - Shantha Kumara

1 Answers

2
votes

You need to define DRUPAL_ROOT

define('DRUPAL_ROOT', $_SERVER['DOCUMENT_ROOT']);
require_once($_SERVER['DOCUMENT_ROOT'] . "/includes/bootstrap.inc");
drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);

Edit://

Having quickly been playing with this myself, the session won't load your user property properly because the change in $base_url

You could fix this by using this code, before you initialise the DRUPAL_BOOTSTRAP

global $base_url;
// Assuming your drupal installation lives in the DOCUMENT_ROOT
//  -- Which yours does
$base_url = (array_key_exists('HTTPS', $_SERVER) ? 'https://' : http://') . $_SERVER['HTTP_HOST'];