Link to root directory

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
kinger88
Forum Newbie
Posts: 12
Joined: Thu Apr 09, 2009 10:08 am

Link to root directory

Post by kinger88 »

Hi all,

Can anyone tell me a way of directly linking to the root directory (the public html folder) ?

I've tried $SERVER["DOCUMENT_ROOT"] but this gives the directory this code is in rather than the public html folder.

I want to use this in an included footer, which links to the index but on some pages it would need to be ../index.php and in others it would need to be ../../index.php etc etc so I need a way of linking to the route directory from which ever directory I am currently inside.

Hope I have explained that well enough.

Cheers
Steve
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Link to root directory

Post by requinix »

Print out $_SERVER and see if there's something in there that looks useful.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Link to root directory

Post by pickle »

Start all your urls with /.

For example:

Code: Select all

<a href = "/index.php">Home</a>
will work on a page at http://www.yourdomain.com/dir/page.html, and also http://www.yourdomain.com/a/few/levels/in/page.html
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
kinger88
Forum Newbie
Posts: 12
Joined: Thu Apr 09, 2009 10:08 am

Re: Link to root directory

Post by kinger88 »

Thank you, I was making it more complicated than it is!
keevitaja
Forum Newbie
Posts: 8
Joined: Wed Feb 25, 2009 7:46 am

Re: Link to root directory

Post by keevitaja »

Code: Select all

define(DIR_SEP, DIRECTORY_SEPARATOR);
define(PATH, dirname(__FILE__).DIR_SEP);
 
$url_path = str_replace(str_replace('\\', '/', $_SERVER["DOCUMENT_ROOT"]), '', str_replace('\\', '/', PATH));
$url_path = (ereg('^/', $url_path)) ? $url_path : '/'.$url_path;
define(URL_PATH, $url_path);
unset($url_path);
Post Reply