Connecting Tech Pros Worldwide Help | Site Map

Hiding a PHP app behind a symlink?

Patrick Lioi
Guest
 
Posts: n/a
#1: Jul 16 '05
I've developed a simple content management system in PHP, and I'd like
to be able to put the code for it in a single place, and have several
sites on one server seamlessly share the code.

I want to put the app itself in /DOCROOT/MyCMS/.

I want to put a site that uses it in, say, /DOCROOT/site1,
/DOCROOT/site2, etc.

I want to place a symlink to /DOCROOT/MyCMS/ inside each site
directory, so /DOCROOT/site1/MyCMS/index.php is reachable as expected.

Here is the problem. Each individual site will need a separate config
file. So /DOCROOT/site1/ will contain both the symlink to the CMS,
and a config.php file.

I want code in /DOCROOT/MyCMS/ to be able to
require_once("../config.php"). But I'm new to symlinks, and a little
testing has shown me that unix command ls seems to disagree with
itself about how to follow symlinks "backwards". Do I need to take any
special action to make require_once() follow the symlink "back" to the
proper /DOCROOT/siteN/ directory?
Jason Dumler
Guest
 
Posts: n/a
#2: Jul 16 '05

re: Hiding a PHP app behind a symlink?


Patrick Lioi wrote:[color=blue]
> I want to put the app itself in /DOCROOT/MyCMS/.
>
> I want to put a site that uses it in, say, /DOCROOT/site1,
> /DOCROOT/site2, etc.
>
> I want to place a symlink to /DOCROOT/MyCMS/ inside each site
> directory, so /DOCROOT/site1/MyCMS/index.php is reachable as expected.
>[/color]

You might want to do something like this:
Read the currently running script's directory from the $_SERVER vars.
Based on that, determine where the config file is located.
i.e.
<?
$arr_path = explode("/", $PHP_SELF);
include_once($DOCUMENT_ROOT .
"/" . $arr_path[0] . "/" .
"MyCMS/config.php");

?>

Jason

Centurion
Guest
 
Posts: n/a
#3: Jul 16 '05

re: Hiding a PHP app behind a symlink?


"Patrick Lioi" wrote...

*SNIPPED*
[color=blue]
> I want code in /DOCROOT/MyCMS/ to be able to
> require_once("../config.php"). But I'm new to symlinks, and a little
> testing has shown me that unix command ls seems to disagree with
> itself about how to follow symlinks "backwards". Do I need to take any
> special action to make require_once() follow the symlink "back" to the
> proper /DOCROOT/siteN/ directory?[/color]

Make all your symlinks absolute, not relative and you should be fine :-)

ie,

ln -s /abs/path/to/file /abs/path/to/symlink

That will get around the problem of having relative symlinks failing but it
could make you symlinks a few bytes longer - considering symlinks are
usually much smaller than the files you're linking this shouldn't be a
problem. But I gather the main aim here is to only have to edit a single
instance of the core "library" functions etc for each site, in which case
yes, symlinks are the way to go.

If all else fails, and the libraries and sites are all on the same file
system you could use hard-links (ln without the -s). These are a bitch to
clean up though coz they "look" like regular files, but are actually
referencing the same inode (hard to track down all the hard links etc).
"man ln" is your friend :-)

-- James
_______________________________________
A random quote of nothing:
"... I should explain that I was wearing a black velvet cape that was
supposed to make me look like the dashing, romantic Zorro but which
actually made me look like a gigantic bat wearing glasses ..."
-- Dave Barry, "The Wet Zorro Suit and Other Turning
Points in l'Amour"


Closed Thread


Similar PHP bytes