ID:277633
 
I have my web server laid as followed.

/
usefulfunctions.php
index.php
test/ (directory)


/test/ <- In the directory
index.php

in test/index.php I have the following.
require_once('usefulfunctions.php');
echo 'Hey world!';


My problem is, I need to be able to access usefulfunctions.php from everywhere! I was thinking I could just do erquire_once('http://ip:port//usefulfunctions.php'); but that's really bad pratice, isn't there another way I could do this?!
Right, but then if I include the file in another php document, it won't read the directory right.
My method for handling this is by setting a variable to the path to the root directory in the file that includes it, and then basing all directory assumptions from that.

So, for example, say I had a "stuff.php" and two "index.php" files including it, set up something like this:
stuff/stuff.php
other/index.php
index.php


stuff/stuff.php would expect that this root directory variable (call it "rootDir" for now) is already set by the time it is included. If it includes other files, it can base the directory on this preset rootDir value. For example:

// stuff/stuff.php:

require_once($rootDir . 'otherStuff/otherStuff.php');
// ...


Now, you want to use a hierarchy for inclusions. stuff/stuff.php expects rootDir to be set already. But the index.php files? I find it easiest to set rootDir in these files, then to start including files, like so:
// other/index.php:

$rootDir = '../';
require_once($rootDir . 'stuff/stuff.php');
// ...

// index.php:

$rootDir = ''; // set it so that included files using rootDir are valid
require_once('stuff/stuff.php'); // i could pre-append $rootDir, but there's no need...
// ...


Anyhow, that's one method I've come up with personally to handle the problem you're talking about.

Hiead