Dan
Dan

Reputation: 3815

Absolute Path for Shared Servers and Local Servers with PHP for CSS

I have searched for a one stop fix I can use across multiple projects but i haven't found anything to work quite yet.

So when I build a site, i use PHP includes for the headers, nav, and footer of site that are used across the site.

so for a template file that will include the header, nav, body, and footer, i do:

<?php include 'views/headerHTML.php'; ?>

<?php include 'views/headerNav.php'; ?>

<div id="main">

<!--content-->

</div><!--main-->

<?php include 'views/footer.php'; ?>

Now my CSS call lines are located in the headerHTML along with any JS or tracking snippets.

if a webpage is located within multiple folders - then the includes have to change relative to the root files

<?php include '../../views/headerHTML.php; ?>

but the CSS & JS references within that include then becomes incorrect as it is written relative to ROOT

<link type="text/css" rel="stylesheet" href="css/test.css" />

I use WAMP for local and all my projects are based on shared virtual servers with multiple providers (GD, fatcow, etc.) and they all have their own wacky root references and such.

I tried to use solutions such as

$_SERVER['DOCUMENT_ROOT']
define('ROOT', dirname(__FILE__));

But i cant seem to get them to work across my local server and their virtual servers. All i want is that no matter how deep my webpage is within the folder heirarchy - the references within the header, nav, footer that reference code snippets or utils that are located in their respective folders will properly be called.

What i have been doing is setting specific includes that have the references within them changed to work so:

<?php include '../../views/headerHTML_DoubleFolder.php; ?>

Which is meant for a page that is two folders deep.

I am 100% sure that is probably the worst thing i can do and negates the purpose of doing includes in the first place

Someone make a fool of me and teach me something new please =D !

I appreciate it.

EDIT ---------------

Example of the problem =

localhost/site/login/login.php calls:

<?php include '../views/headerHTML.php'; ?>

And within that headerHTML.php file there is:

<link type="text/css" rel="stylesheet" href="css/style_v0313.css" />

And as you can see the css location will not be correct when the login.php calls it via the headerHTML

adding / or ./ or dirname(FILE) to the css href still will not load the style sheet when visiting login.php

Upvotes: 1

Views: 988

Answers (2)

Matheus Lopes
Matheus Lopes

Reputation: 83

Try to use the "./addresshere".

Example domain: www.test.com
Suppose your website is on a folder like: www.test.com/portal
and your css is on: www.test.com/portal/css

CSS call example:

<link type="text/css" rel="stylesheet" href="./portal/css/test.css" />

The ./ let you go by the root (main) directory. On localhost the ./ gives you the localhost address

Upvotes: 2

Connor Gurney
Connor Gurney

Reputation: 690

You can use paths, like /css/style.css.

Upvotes: 2

Related Questions