1

I'm new to PHP and I'm having a problem when trying to link my CSS files using include.

Basically I need my files to link back to a certain directory no matter how far down the file is. I have tried using

<?php
    include $_SERVER['DOCUMENT_ROOT'] . '/sysprogs/required/header.html'; 
?>

But header.html contains the links to my css files so the directory it ends up looking for the css files in is

http://localhost/SysProgs/software/CSS/style.css

instead of where I want it to go to which is

http://localhost/SysProgs/required/CSS/style.css

I hope that made sense and I hope you can help me

Thankyou for all your help everyone!

5 Answers 5

7

I would definitely not use <base>. I've run into many problems with this before. If you use <base>, ALL of your links will become relative to that base value.

Instead, I would recommend setting PHP constants for common directories. For example:

PHP Code:

<?php
    define('CSS_DIR', '/SysProgs/required/CSS/');
?>

HTML Code:

<link href="<?php echo CSS_DIR ?>style.css" rel="stylesheet" type="text/css" />
Sign up to request clarification or add additional context in comments.

5 Comments

I actually tried this on my hosting and I am getting these errors: keironlowe.host56.com/software What do they mean?
Looks like you're missing a slash somewhere. /usr/local/apache/htdocsrequired/intro.php There should be a slash after htdocs.
Yea I've looked through my code and that is nowhere to be seen
Ok there was a slash missing my bad, I've fixed it but it still comes up with errors
Looks like a problem with permissions. Check out this link for more info. You may want to post another question about this error.
3

One Idea

Use the full URL in header.html. This will be unambiguous and robust.

<head>
<link href="/FULL_BASE_URL/style/style.css" rel="stylesheet" type="text/css" />
</head>

Another Idea

Use the <base> header tag. This allows you to specify a base URL for links, including CSS, and may require the least work in the short term (see note below).

<head>
<base href="FULL_BASE_URL" />
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>

More at w3schools

Note: As is noted in the comments below base may ultimately cause more confusion than it is worth.

4 Comments

+1. In my opinion, you should always use absolute paths in your HTML when including scripts and CSS; unless you're building a high-portability script for distribution, using absolute paths means you never have to second guess whether or not a resource file will load.
base is bad because leads to a spaghetty code, while absolute path ia always unambigous
You guys are right. I've edited my response to include a warning. Thanks.
<base> is horrible because it affects all non-absolute urls in your document.
2

I like to define both an absolute path and a webroot in a central place in your application:

<?php

  define("APP_WEBROOT", "/myapp"); 
  define("APP_ROOTDIR", "/home/www/example.com/htdocs/myapp");

 ?>

you can then "absolutize" the correct links like so:

<?php echo APP_WEBROOT; ?>/software/CSS/style.css

I prefer this

  • over <base> because that tag creates confusion and makes code harder to maintain in the long run

  • over using absolute paths /software/CSS/style.css because those make you unable to install your application in a subdirectory of your choice: You will always be bound to the root directory.

7 Comments

Do you use the first one in your projects?
@Col not in this exact form but in principle, yes (I often use a conf() function). Why?
i think it's redundant. your app's webroot is not too often differs from just / to use such a construct <?php echo APP_WEBROOT; ?> for the every link on your site
@Col it depends. I sometimes write web apps that are distributed as a package and need to be able to be installed in a directory other than web root (www.example.com/myapp/version2/...). For that scenario, I have not found a simpler solution yet than to explicitly define the base paths - DOCUMENT_ROOT won't work here. (I'm not too fond of it myself, I'm always interested if there's a simpler idea.)
There's no point in adding the http://.../ part to your path. Your webroot should start with a trailing slash. This allows compatibility with different hostnames.
|
0

I run into this problem a lot when designing sites. When I have custom CMS, I use the following:

$basedir = "root_directory/";
$basedirPHP = $_SERVER['DOCUMENT_ROOT'].$basedir;
$basedirHTML = "http://".$_SERVER['SERVER_NAME'].$basedir;

I define $basedir so I can move the site to different subdirectories in the server without any effort. The $basedirPHP and $basedirHTML are so I can call on files either with php, or like you mentioned, when linking CSS, JS, etc.

If you're on wordpress, just use the good ol' bloginfo('template_directory'); to do the same in template files.

1 Comment

I would refrain from including the CSS directory in whatever variable you use, simply because you won't have the flexibility of using the variable for files in other directories. That's just my preference, though. Nothing's stopping you from having a variable for each directory you're using.
-2

The first thing for you to understand, is your question has nothing PHP related. It is as simple as just filenames in your HTML questuon. Without PHP it will remain the same. Just use absolute path to your CSS file

And another thing to think of: consider to accept some questions.

1 Comment

Voted down because the question specifically asked how to reference static paths in a portable manner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.