Page 1 of 1

Absolute Path??

Posted: Tue Oct 20, 2009 7:10 am
by prue_
hi! I have a website with like 3 directories..
1.root
2.level1
3.level2

is there a way I can only have 1 header and footer? currently I have a set of 3: like below

(relative url)
root -- src="images/image.gif"
level1 -- src="../images/image.gif"
level2 -- src="../../images/image.gif"

root -- href="index.php"
level1 -- href="../index.php"
level2 -- href="../../index.php"

I don't think using absolute url (http://www.domain.com/) will be a good idea.. is there any other way?
like define ('ABSOLUTEPATH./') i'm not sure of this code..

thanks a lot!

Re: Absolute Path??

Posted: Tue Oct 20, 2009 9:28 am
by Reviresco

Code: Select all

<img src="/images/image.gif" />
<a href="/index.php"></a>
or for the second one:

Code: Select all

<a href="/"></a>

Re: Absolute Path??

Posted: Tue Oct 20, 2009 10:11 am
by el_gato
Hi, to solve the problem is to create one file that define the root url, than include it in all of your files.

Create one file, called init.php and place in the top of your root directory, ex: /home/foo/init.php (/home/foo is your root directory)

Code: Select all

 
define('ROOT_DIR',      dirname(__FILE__));
define('ROOT_URL',      substr($_SERVER['PHP_SELF'], 0, - (strlen($_SERVER['SCRIPT_FILENAME']) - strlen(ROOT_DIR))));
 
In every files on whatever directory it is, include init.php, ex, a file in /home/foo/mod/index.php:

Code: Select all

 
<?php
include '../init.php';
?>
 
root -- src="<?php echo ROOT_URL;  ?>/images/image.gif"
level1 -- src="<?php echo ROOT_URL;  ?>/images/image.gif"
level2 -- src="<?php echo ROOT_URL; ?> /images/image.gif"
 
root -- href="<?php echo ROOT_URL; ?>/index.php"
level1 -- href="<?php echo ROOT_URL; ?>/index.php"
level2 -- href="<?php echo ROOT_URL; ?>/index.php"

Re: Absolute Path??

Posted: Tue Oct 20, 2009 8:32 pm
by prue_
thanks el_gato, you almost got it but it removes 1 letter on my directory... I put .'/' after dirname(__FILE__)

define('ROOT_DIR', dirname(__FILE__).'/');

now it's working.... thank you very much!