views:

115

answers:

5

Hi, 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!

+2  A: 

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.

Geoff
+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.
Dereleased
base is bad because leads to a spaghetty code, while absolute path ia always unambigous
Col. Shrapnel
You guys are right. I've edited my response to include a warning. Thanks.
Geoff
`<base>` is horrible because it affects all non-absolute urls in your document.
Kenaniah
A: 

Hey Kerion.

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.

Col. Shrapnel
Voted down because the question specifically asked how to reference static paths in a portable manner.
Kenaniah
+1  A: 

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.

Pekka
Do you use the first one in your projects?
Col. Shrapnel
@Col not in this exact form but in principle, yes (I often use a `conf()` function). Why?
Pekka
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. Shrapnel
@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.)
Pekka
@Col I think Wordpress analyzes DOCUMENT_ROOT and compares it to the current directory, to find out at which point in the folder hierarchy it resides and where the root dir is. I've never looked closely at it but that sounds like a way to find it out automatically. (It would still require adding a constant to every link.)
Pekka
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.
Kenaniah
@Kenaniah you have a point. There are setups where a host name makes sense but they are rare.
Pekka
+2  A: 

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" />
Bryan Downing
Thankyou so much, this way is so simple and works perfectly!
Keiron Lowe
I actually tried this on my hosting and I am getting these errors:http://keironlowe.host56.com/software/What do they mean?
Keiron Lowe
Looks like you're missing a slash somewhere. `/usr/local/apache/htdocsrequired/intro.php` There should be a slash after htdocs.
Bryan Downing
Yea I've looked through my code and that is nowhere to be seen
Keiron Lowe
Ok there was a slash missing my bad, I've fixed it but it still comes up with errors
Keiron Lowe
Looks like a problem with permissions. Check out [this link](http://www.mydigitallife.info/2007/03/15/php-scripts-open_basedir-restriction-in-effect-error/) for more info. You may want to post another question about this error.
Bryan Downing
Thanks for your help, you've been fantastic!
Keiron Lowe
It would be good if you would accept Bryan's answer, Keiron.
Geoff
Sorry I forgot ^^
Keiron Lowe
Thanks Geoff and Keiron! =)
Bryan Downing
A: 

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.

Erin
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.
Erin