Php include

The recent talk of Portal and the php include command got me thinking and playing.

This page: https://www.ci-clientservices.com/bespoke/ (scroll down below the header video)

Pulls in the content from this page: https://www.ci-clientservices.com/bespoke/content/

But, the formatting of the content page is lost.

Where am I going wrong? Or, have I misunderstood how the php include thingie works?

The page you are including, is pulled in, in its entirely. That includes all the CSS / Javascript links at the top of the page too.

But because those links are relative, they break when you try to access them from elsewhere in the website. For that reason, your HTML / content works fine, but the links to the CSS are broken. Hence the styling is lost.

So basically on the webpage you are importing / including content into, you need to ensure the same CSS is available.

Typically you only do includes on small segments of code, not entire webpages. Because you do not need the HTML, body, head and all that other stuff again.

File to be included (my_header.html):

<i>Hello World!</i>

The page you are including the content on:

<?php include("somewhere/my_header.html"); ?>
<br>
It's only 3 more sleeps 'till Christmas!

The end result:

Hello World!
It’s only 3 more sleeps 'till Christmas!

5 Likes