Search code examples
phpcssrequire-once

Absolute Address for CSS


I have the following file structure on my PHP localhost:

  • Root
    • css/
      • main.css
    • admin/
      • admin.php
    • index.php
    • header.php

Both admin.php and index.php are requiring header.php, in which main.css is included like the following:

    <link rel="stylesheet" type="text/css" 
        href="<?php echo $_SERVER['DOCUMENT_ROOT']; ?>/css/main.css" />

However, the css file isn't included properly. When I look at the Source Code in Chrome on Mac, The browser has turned that into

http://localhost/Users/ljhljh235/Documents/web/hetd/css/main.css

in which http://localhost is not intended to be here. Could anybody help me on how to build the correct absolute path for the css file?

P.S. My way of including header.php in admin.php and index.php is

require_once ($_SERVER['DOCUMENT_ROOT'].'/header.php');

And I'm using MAMP 2.1.1.

Thanks for any help.


Solution

  • The variable $_SERVER['DOCUMENT_ROOT'] is for server side path, use /css/main.css only, it will be relative to the domain ie : http://domain.com/css/main.css