Absolute Path??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
prue_
Forum Commoner
Posts: 64
Joined: Thu May 07, 2009 10:34 pm

Absolute Path??

Post 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!
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Absolute Path??

Post 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>
User avatar
el_gato
Forum Newbie
Posts: 12
Joined: Sun Oct 18, 2009 9:28 pm
Location: Bandung
Contact:

Re: Absolute Path??

Post 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"
prue_
Forum Commoner
Posts: 64
Joined: Thu May 07, 2009 10:34 pm

Re: Absolute Path??

Post 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!
Post Reply