Bentley Carr
Bentley Carr

Reputation: 702

PHP Include - full url

I've been using the php include function for my navbar for my website. It works well but.... My HTML says

<?php include 'include/navbar.html'; ?>

Now if I have a HTML page in /techpages/toptech.html so I would like to change my php include to <?php include 'http://example.com/include/navbar.html'; ?>. The problem is I get heaps of errors then.

Can someone help me? Can someone answer one of these questions?

  1. How can I make the PHP include function work with a full URL?
  2. HTML has ../ to go into the parent folder. Is there a CSS equivilant?

Any help would be much appreciated.

Upvotes: 1

Views: 969

Answers (2)

luk2302
luk2302

Reputation: 57124

Here are two approaches which I think will work for what I imagine you are trying to achieve:

  • You could include via include $_SERVER['DOCUMENT_ROOT'].'/include/navbar.html'; - this will always include the same file regardless of in what file in what directory you put the include
  • PHP has an include_path which specifies where to look for file-includes, you can add the /include directory in this include_path and from there on always include via include 'navbar.html'. But to be able to do this you have to have permission to access/modi the php.ini...

Upvotes: 1

user2441590
user2441590

Reputation:

One of the options of doing that is:

include($_SERVER['DOCUMENT_ROOT']."/include/navbar.php");

Upvotes: 0

Related Questions