Question

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!

Was it helpful?

Solution

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" />

OTHER TIPS

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.

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top