Forum Moderators: coopster

Message Too Old, No Replies

php server side includes

related to sub-directories

         

TampaLou

7:07 pm on May 19, 2004 (gmt 0)

10+ Year Member



I'm doing my first big project for a website using php with server side includes. No biggie, except that when I try to use the ssi in a sub-directory, it doesn't want to cooperate. At all.

<?php include "includes/header.php";?>

That works just fine when it comes to files in the main directory. But that was overcrowded, and I wanted to create sub-directories for expanding the site. However, attempting to do these following techniques failed to provide the desired result with files in the sub-directories:

<?php include "/includes/header2.php";?>
<?php include "../includes/header2.php";?>
<?php include "http://www.****.com/includes/header2.php";?>

Anyone have any ideas on what I should do? Thanks for any help you could provide...

Lou

jatar_k

7:49 pm on May 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



always remember that includes work on the filesystem and, therefore, don't know anything about the website root. All they know is they are in some directory somewhere.

for a tags / == website root
for includes / == server root

PHP Predefined Variables - $_SERVER [ca.php.net]

so we need to have includes with an absolute path. This way you can move them anywhere within the site and they won't break

to reference the site root we can use
$_SERVER['DOCUMENT_ROOT']

so
<?php include $_SERVER['DOCUMENT_ROOT'] . "/includes/header2.php";?>

there are a ton of other useful variables in the $_SERVER array as well

TampaLou

8:36 pm on May 19, 2004 (gmt 0)

10+ Year Member



Thanks so much Jatar -- that worked like a charm!

Lou