How to use PHP Include for Different...

User 2660090 Photo


Registered User
89 posts

I am having problems at the moment using a PHP include effectively.

This stylesheet, when accessed from the index.php file in the main directory works fine.
The problem is I try and access it from the buy and contact us pages. Because these files are in
folders, it means that the CSS link on the left is not relevant for those pages. Obviously if I change it to:
"../css/style.css", that would work for the buy and contact us pages but not the home page in the main directory.

This is an example of the buy page (also relevant to the contact us page).

http://oi57.tinypic.com/2eauzbr.jpg

This is the PHP include I use on my home page index.php.

http://oi60.tinypic.com/kesn4z.jpg

And this is the PHP include I use on my buy and contact us pages.

http://oi61.tinypic.com/10qgdgo.jpg

This allows the respective pages to link to the head.php, but obviously because the actual links in the head.php don’t change at the moment, it means it cannot work for the buy and contact us pages.

Using the example of the css/style.css line of code, how can I get it so this link is relative so it’ll work no matter where the page is housed? I don’t want to use absolute links because they’re a pain to change if need be.
User 187934 Photo


Senior Advisor
20,245 posts

Give this try.
include $_SERVER['DOCUMENT_ROOT'].'/css/style.css';
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com

Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.