14 June 2008

Including PHP includes on a Drupal multisite set up

I'm adding this snipped just in case it helps someone else, or maybe someone else might have a better solution and share it. Either way, it's good to share.

Drupal is set up on our servers as a 'Multisite' set up... so that several domains/web sites all use the core software but have individual things like themes (how things look) and modules (various ways of doing things) that are for each individual site. I've set up a testbed type site, and I'm now testing for doing things like PHP programming so I can include some of the stuff I use on my personal site. That way once I work out the kinks, I can convert my website to use Drupal too.

The hard bit has been being able to use 'include' files that can be used many times over by my software. The big issue is where to put these files so they are safe, and yet so the web server and the Drupal software can find it, while keeping it separate from the other Drupal websites and the core software. I tried lots of things to get this to work.

There's a command for Apache (the webserver) to tell it where to look for PHP include files. This would only work if I put it in the main file that affects all websites, so that wasn't a good solution. I tried using an .htaccess file in the main folder but that didn't work - the command is ignored for security reasons, as it should be).

In the end, it turned out to be a variable defined at the top of my web page that looks like:
  • $inc_dir = "/PATH/TO/CUSTOM/INCLUDES/HERE";

and then where I want to include the file in my code, I just have:

  • include("$inc_dir/FILENAME.inc");

That's it. Very simple. Now I can go on to sorting more complex issues out. If you know of a better way to do this, please leave a comment. I'd be happy to hear from you.


No comments: