Page 1 of 1

How do I make my website pages location independent?

Posted: Tue Aug 16, 2011 11:37 am
by webfeetdesign
Hi there,

First off, i'm total newby at php. I know basic functions such as <?php include and thats about it really!!!

My pages are set up the following way...

1. I have a /common folder which has the header.php and footer.php and any other 'common' pages I need.
2. I then have my index.php file which calls the header and the footer.php from the /common folder.
3. I've also created a new folder (/newfolder) which has a file in /newfile.php so from the root its - newfolder/newfile.php
4. I also have an images folder which the files are linked to from each page I create.

My problem is that this changes from page to page as they are in different directories and different levels.

Is there a best way to do this so I don't have to use ../folder/file.php? I've tried /folder/file.php but I get the following error - Warning: include(/common/header.php) [function.include]: failed to open stream: No such file or directory in /homepages/27/d194522123/htdocs/webfeetdesign/testing/lwk-tests/white-layout/landing.php on line 12

To see the mess i've created go to http://xxxxxxxxxxx.com

Hope someone out there can understand my gibberish!!!

Many thanks

Alex

Re: How do I make my website pages location independent?

Posted: Tue Aug 16, 2011 1:52 pm
by Jonah Bron
So what you're saying is that newfolder/newfile.php needs to refer to files in common/ ? In that case, to use absolute URLS you'll need to create a file in a known location that knows where it is, like this:

/config.php

Code: Select all

<?php
define('SITE_ROOT', dirname($_SERVER['SCRIPT_FILENAME']));
?>
Then from whatever file you need, do this:

Code: Select all

<?php
include('config.php'); // or ../config.php, whatever is necessary
include(SITE_ROOT . '/common/header.php');
?>