Agaric Design Collective

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

Average: 4 (1 vote)
By Benjamin Melançon
on 30 Dec
3 comments

Key words and phrases

access Drupal bootstrap current user's session from internal folder

Tags

Description

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

 

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!

Posted by Wim Mostrey (not verified) on Fri, 2009-02-20 08:08
Cool!

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

Posted by Benjamin Melançon on Fri, 2009-02-20 08:51
Job well Done

Thanks Benjamin,

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

Regards

Posted by chavan (not verified) on Sat, 2009-07-18 13:40
Post new comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <small> <pre> <strike> <sub> <sup> <kbd> <s>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.