wesley tanaka

Including Drupal from a stand-alone PHP file

‹ Rational | Getting java sound to use ALSA default device ›
()

It's been a while since I've wanted to use stand-alone PHP files and call drupal from those files. There are two cleaner ways to do such a thing:

  1. create 'page' nodes with PHP input format
  2. create a module and define the hook_menu() callback
Although not all of the functionality of Drupal will be available, there are a few advantages to using stand alone PHP files:
  1. Doesn't require space in the database -- free PHP-enabled hosting providers seem to have small database quotas independent of your filesystem quota.
  2. Uses the filesystem to lookup what code to run for a certain URL.  This is likely to be faster than a Drupal path alias lookup in the database.
  3. Every enabled module uses up memory on every page view, so if you can avoid creating a module by using php files, your PHP processes will have a smaller memory footprint, possibly making your web server faster or able to handle more users.

The way I'm doing it is something like:

<?php
chdir('/full/path/to/top/drupal/directory/');
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// so that drupal_is_front_page() returns false, and probably other reasons
$_GET['q'] = 'fake';

// call other drupal functions like drupal_set_title() or drupal_set_breadcrumb() here if you like

echo theme('page', 'the HTML contents of your page go here');
Syndicate content