require_once() and include() using ROOT

Hey guys,

I am trying to figure out the closest way to replicate the the HTML href attribute in PHP, in the following way…


The above code works for any page located anywhere on the website because it starts at the root (using the slash –> / in the begging of the link).

How can I do this with PHP’s require_once() and include() functions?
Obviously doing <code>include(‘/folder/file.php’);</code> doesn’t work… Instead I’ve been forced to use direct relative linking [e.g. include(‘…/…/…/folder/file.php’);]

When using includes and such I find it easier to do docroot:

$docroot = $_SERVER['DOCUMENT_ROOT'];

That way…even if you change hosts you might not have to change the code on all the pages that use this.
Hope points to alternative,

Great. Thanks a lot Kevin!!

Would it pose any problem to use that on just about every page of a website? And given that a website has like 1000 pages?

Also, what advantage is there to declaring a variable for the document root instead of just doing <code>require_once ($_SERVER[‘DOCUMENT_ROOT’] . “/global.php”);</code>

One more thing, what do you think is faster? And is it MUCH faster? Given that both of these work for a page…

require_once ($_SERVER[‘DOCUMENT_ROOT’] . “/forum/global.php”);
require_once (“…/…/forum/global.php”);

Like I stated, yes, you put it on all the pages at the top where you need it and you (usually) won’t have to change it as every host has a document root (or almost every host) if they use apache or have access to htaccess.

It’s a fire and forget method. So that on every document you create all you have to do is:


and so on.

I really do find it easy to go from one website hosting company to another without modifying all the pages all over again.

Is worth doing IMHO.


They both work but if you change hosts, you may have to change the code again anyway. You could really do either or.

Thanks pal!

You’re welcome. I do use it heavily on my site and it’s worked for the last several years, even when I switched hosts like 10 times…lol