User login

Access Drupal functions and session from a PHP script in a site subdirectory

Searched words: 
access Drupal bootstrap current user's session from internal folder

If you want to use Drupal sessions and the logged in user object from a straight PHP script not at Drupal root but somewhere in subdirectories in a site, you will probably need to do something like I endend up doing in profiles/scf/switch.php to impersonate the Drupal site root directory.

In fact, I think the partial installation profile idea might need something like this.

(Skip down to resolution.)

Some test output code used to figure out what the domains and sessions were doing:

<?php
// global $user;
// print var_export($user,TRUE);
global $cookie_domain;
// print $cookie_domain;
print '?' . $session_name . '!!';
print session_name();
print 'cookie!!!' . $_COOKIE[session_name()]; // $cookie_domain;
?>

We started out trying minimal... it didn't stay that way.

<?php
/*
  // Minimum load of components.
  drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
//  require_once './includes/file.inc';
  include_once './includes/module.inc';
  drupal_load('module', 'system');
*/

/*
require_once './includes/install.inc';
require_once './includes/file.inc';
require_once './modules/system/system.install';

// Load module basics.
include_once './includes/module.inc';
$module_list['system']['filename'] = 'modules/system/system.module';
$module_list['filter']['filename'] = 'modules/filter/filter.module';
module_list(TRUE, FALSE, FALSE, $module_list);
drupal_load('module', 'system');
drupal_load('module', 'filter');
*/
?>

Resolution

<?php
/**
* In which we jump through some hoops to make Drupal treat
* profiles/scf/switch.php just like the root-level index.php or update.php
*/
// remove '/profiles/scf' from current working directory and set our directory
$base_dir = substr(getcwd(), 0, -13);
chdir($base_dir);
// ini_set('include_path', '.:' . $base_dir); - use of ./ makes this not work
global $base_url;
// duplicated from bootstrap.inc conf_init().
    // Create base URL
    $base_root = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';

    // As $_SERVER['HTTP_HOST'] is user input, ensure it only contains
    // characters allowed in hostnames.
    $base_url = $base_root .= '://'. preg_replace('/[^a-z0-9-:._]/i', '', $_SERVER['HTTP_HOST']);

    // $_SERVER['SCRIPT_NAME'] can, in contrast to $_SERVER['PHP_SELF'], not
    // be modified by a visitor.
    if ($dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/')) {
      $base_path = "/$dir";
      $base_url .= $base_path;
    }
$base_url = substr($base_url, 0, -13);

require_once './includes/bootstrap.inc';

// we need the $user variable, theming...  i think we have to go all the way
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); // DRUPAL_BOOTSTRAP_CONFIGURATION

drupal_maintenance_theme();
?>

Comments

Perfect solution for amfphp

I was breaking my head over how to initiate Drupal with amfphp, which is located in amfphp/services/myservice/myservice.php. I only had to adjust a couple of lines (the ones relating to your specific switch.php case) and it works perfectly now. Thanks a lot!

Cool!

I love it when solving problems solves other problems...

Job well Done

Thanks Benjamin,

i was working on it for such long time. great work

Regards

Very Awesome

Thanks!

I was dealing with this problem for only about 15 minutes before I decided to look online and found your tutorial, but after I gave it a shot, everything was up and working in 5 minutes. This was a great little code snippet for sure. Thanks for posting it online!

Thanks!
John Murray

Thank you for this post; it

Thank you for this post; it saved me quite a bit of headache.

Thanks so much, I have been

Thanks so much, I have been wracking my brain trying to figure out how to do this for a few hours now...

Great post

I spent about an hour trying to figure this out before I found your post. Thanks!

Drupal 7

For drupal 7 you should add

define('DRUPAL_ROOT', getcwd());

Right above the require bootstrap function. It was introduced in D7, and the snippet will fail without it.

Thanks for the snippet !

vente privéé lancel

Jetez un oeil à la liste craigs 1er conservent néanmoins la chasse, lorsque nous possédons une très peu de techniques supplémentaires pour un individu. l'entreprise Formateur en particulier des ménages en cuir ont été la création de niveau de la sacs lancel pas chers marchandise de qualité de la taille et de soldes 2012 s pour soldes Lancel s et Fe soldes Lancel s en raison du fait qu'il est invention. De l'aide de quelques-uns de renommée endosseurs super star, plus le

lancel bb

marketing de bouche invo Lancel style de ING conscients la plupart des femmes, Types de ce type de bacs est généralement à paillettes, à la main, lacées, ou peut-être tout simplement bacs avec des images élaborées ainsi que artsy. Peu importe ce sac lancel BB que vous décidez de style, il est facile de s'attendre à bacs. Les soldes suivants idéale hiver 2012 lors de l'acquisition soldes sac. Ils sont trompés par des termes intelligentes sur des sites qui peuvent être de la publicité de ce genre de bacs à faible coût. Ne vous laissez pas étonné espérons site Embrayages sans aucun doute sont un besoin fondamental pour les

vente privéé lancel

dames. Cela permet Fe soldes Lancel s pour obtenir rudiments de son partout à chaque passe en peu de temps, confort et facilité, et le design.
http://www.lancel-boutique.com/

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or "class="OPTIONS" title="the title".
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.