views:

66

answers:

4

I'm working on my first website using php. I came up with a good way, or so I thought, to deal with including the css sheet depending on where I am. For example, I have a form that I submit to with this code:

 $root = '../';


 function died($error) 
 {
  include '../includes/header.php';
  echo "We are very sorry, but there were error(s) found with the form your submitted. ";
  echo "These errors appear below.<br /><br />";
  echo $error."<br /><br />";
  echo "Please go back and fix these errors.<br /><br />";
  include '../includes/footer.php';
  die();
 }

I set the variable $root to have "../" and then here is the relevant part of header.php:

<link href="<?php echo $root;?>styles/styles.css" rel="stylesheet" type="text/css" />

I would think that it would put in "../" in front of styles, but it doesn't put anything. Why doesn't this work?

Thanks!

A: 

You can make the path to your CSS file absolute:

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

Prefixing a URL with a forward slash (/) will make the URL relative to your site's root. The above example would reference something like http://example.com/styles/styles.css. This way, you can put all your CSS in a folder at the root of your site and not have to worry about the path at all.

mattbasta
I disagree because this makes porting the site to another sub directory much more difficult. Using a variable to define root of this site makes it easier to move the site elsewhere. (I deal with this at work A LOT.)
Chris
@Chris: I'm a professional web developer, currently working at Mozilla. If you include this in your `header.php` file *as the op is doing*, then you only need to modify the value once. Also, sites should only have ONE css file. The whole point is to centralize all of your style information into one file and decrease overhead.
mattbasta
I can't get this to work! <link href="/styles/styles.css" rel="stylesheet" type="text/css" /> in "includes/header.php" it says object not found
JPC
actually it turns out that I have to make it <link href="/sitename/styles/styles.css" rel="stylesheet" type="text/css" />. Guess there's no way aroudn that?
JPC
@JPC: That's the idea: point it at the proper folder as you've done. If the HTML is in your `header.php` or `include.php` file, you only need to change the `href=""` attribute when the directory changes.
mattbasta
thanks! I marked this one as the answer
JPC
A: 

In this situation my suggestion is to use $root = "FULL path";

So for example... if your web root is in "/var/www/html/" and you are working on a project lets call it sampleProject which is in "/var/www/html/sampleProject".

Set $root="/sampleProject/; and this way when you use things such as styles/styles.css"/> you end up with which is what you want.

Unlike other answers this makes your code more portable in my oppinion. For example if you move your site "sampleProject" to /var/www/html/dir1/dir2/dir3/sampleProject/ all you need to do is change $root="/dir1/dir2/dir3/sampleProject/" and it will still work. Whereas if you use explicit full path as some suggest you will have to change this everytime you move your site.

Chris
This does not apply to front-end URLs as the OP has requested. Regardless, this can be better accomplished with use of magic constants such as `dirname(__FILE__)`, which produces a dynamic version of the file path (no hard path necessary).
mattbasta
And my suggestion is not just dealing with URLs. Additionally using dirname(__FILE__) might not be ideal. For example you may want to use css in a parent directory which using dirname(__FILE__) would not help with unless I am missing something.
Chris
In that case, using a `$root` variable is *still* not optimal. The web server is very smart and gives you access to the root: `getenv("DOCUMENT_ROOT")`. Consider: `substr(dirname(__FILE__),strlen(getenv("DOCUMENT_ROOT")))`. It's one thing to reinvent the wheel, it's another to reinvent the wheel as a triangle.
mattbasta
Thanks, I never thought of this before... appreciate the knowledge. :-)
Chris
+1  A: 

$root is not in scope for the function (im assuming..) add global $root; to the top of the function and it should work.

$root = '../';

function whatever() {
  global $root;

  echo "<link rel=stylesheet type=text/css href=\"" . $root . "something.css . "\">";
}
Fosco
If I make it global, is it global to the entire site? What if I have other things defined as $root?
JPC
declaring it as Global in that function means that function will use the globally defined $root. function someFunction(){global $root; //here $root is defined as whatever it was defined //in global context }
Chris
It is not defined inside a function, so it is in the 'main' scope.. If you want to use a variable defined outside of a function, you have to reference it as a global as I said. It does not affect other functions that may have a variable of the same name in that inner scope.
Fosco
A: 

For referencing files such as this you simply need to start the path with a forward slash and the path is then relative to your root directory...

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