1

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!

2
  • 2
    Not to come off as expectant, but I agree with @webdestroya: it only takes a second to mark an answer as accepted. Go back through some of your old questions and mark the best answers as accepted (the check boxes mark answers). I'll be your best friend! I'll give you some gum! Commented Jul 28, 2010 at 17:58
  • Sorry guys, I'm new to this site! I've not had a lot of my questions answered but this one seems to have a lot of answers so I'll go through them! Commented Jul 28, 2010 at 18:00

4 Answers 4

1

$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 . "\">";
}
Sign up to request clarification or add additional context in comments.

3 Comments

If I make it global, is it global to the entire site? What if I have other things defined as $root?
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 }
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.
0

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.

5 Comments

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: 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.
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
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: 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.
0

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.

4 Comments

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).
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.
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.
Thanks, I never thought of this before... appreciate the knowledge. :-)
0

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

Comments

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.