Senne Vandenputte
Senne Vandenputte

Reputation: 539

Best practice for URLs in included PHP files

Until now, I used to put absolute URLs in my included PHP files. For example in navbar.php, with a link to the services page: href="http://domain.com/services.php"

Since navbar.php is included in different directories, I had to use an absolute URL.

Problem is that when I'm testing the site locally using wamp server, this points me to the live site, which is not what I want.

What is the best practice for URLs in included PHP files?

I'm looking for something like 'root/thefile.php', where root can be either the domain (when the site is live) or localhost (when building and testing the site locally).

Upvotes: 0

Views: 367

Answers (2)

Sinf
Sinf

Reputation: 123

include ($_SERVER["DOCUMENT_ROOT"]."/file path");

Upvotes: 0

kerv
kerv

Reputation: 315

$_SERVER['HTTP_HOST']

This will give you the root url ex.(domain.com)

So you could do use this:

echo 'http://' . $_SERVER['HTTP_HOST"];

And this would give you your url. You could also use

echo 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];

This will give you the complete url with the requests in it. ex (domain.com/thefile.php);

Upvotes: 1

Related Questions