I'm having little problems with defining document root...

my config.php file looks like this:
define ("SITE_ROOT",$_SERVER['DOCUMENT_ROOT']");

my index.php css line looks like this:
<link rel="stylesheet" type="text/css" href="<?php echo SITE_ROOT; ?>/css/main.css" />

but the browser shows the page without css.

when i look into the source it shows:
<link rel="stylesheet" type="text/css" href="/home2/trgkohr/public_html/css/main.css" />

thank you for your answer

    define ("SITE_ROOT",$_SERVER['DOCUMENT_ROOT']");

    is probably better formated like so

    $server_doc_root = $_SERVER['DOCUMENT_ROOT'];

    define ("SITE_ROOT",$server_doc_root);

    not only that i discovered a stray " in your define

    define ("SITE_ROOT",$_SERVER['DOCUMENT_ROOT'] -> "<- );

      this mistake I accidentaly have putted in post. It's not the problem in my code. It still won't work

        Document root is based on the server's filesystem, not what domain you're on.

        If you want the root domain, you can use $_SERVER['HTTP_HOST'].

        Note that if you're just doing domain.tld/blah/blah/blah.css or whatever, you can just use "/blah/blah/blah.css" and it will be relative to the domain root. so:

        <link rel="stylesheet" type="text/css" href="http://www.domain.tld/images/test.jpg" />

        is equivalent to:

        <link rel="stylesheet" type="text/css" href="/images/test.jpg" />

        So you don't need the domain name all the time.

          thanks bpat ,now it works

          this is how it looks like now:

          define("SITE_ROOT","http://".$_SERVER['HTTP_HOST']);
          without typing "http://" it won't work.

          But now it works only on the server not on my local pc (wamp). Is there a solution to work in both cases ??? Thats why i wanted to use $_server['document_root'].

            Like I said, if you are just using "http://www.domain.tld/path/to/some/file.css", then just remove the domain stuff. If the href or src attribute starts with a / then it will look for whatever you specify from document root. So like I said above:

            <link rel="stylesheet" type="text/css" href="http://domain.tld/styles/site.css" />

            Is exactly the same as saying:

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

            The difference being that you can move the second one around to any server and as long as the path doesn't change, you will get your stylesheet. The first one should work as well; however, you would be downloading the stylesheet from the web rather than whatever is local to the site (be it locally on your home server or locally when the site is on the live server).

            This is basic HTML.

              yes i know that.

              But i will have some files in the root directory and some in subdirectories. and i want to include html files like header, menu, footer.

              For example in my index.php which is in root directory it will look like this:
              <link rel="stylesheet" type="text/css" href="css/main.css" />

              and in subfolder files it will look like this:
              <link rel="stylesheet" type="text/css" href="../css/main.css" />

              I want to include only one file header.php anywhere in the site.

              do you understand what i mean ?

                Why not use only one css folder and put all your css files into that? What you currently have is more than one css folder... By using only one css folder, one include folder, etc.. etc.. keeps everything nice and neat and easy to find should you need to make changes.

                On my site, I have one css folder, and I use $_SERVER['DOCUMENT_ROOT'] . '/css/css_file_here.css'

                Makes life easier IMO.

                  i have only one css folder. i will have directories on the site like users,admin and files inside and some files in root directory like index.php. And i want to include head tag like <?php require_once(SITE_ROOT."bin/html/head.php"); ?>, everywhere the same tag.

                    Ok.. perhaps I misunderstood.

                    I never bothered with using DEFINE to create a a constant root.. I simply use as an example:

                    <?php require_once($_SERVER['DOCUMENT_ROOT'].'/include/include_file.inc.php'); ?>
                    

                    This works locally as well as live.. (so long as you keep the directory structure identical on both systems).

                      Main point is that you dont have to use SITE_ROOT in css or any other media files that you use. Just use "/css/css_file_here.css", "/js/somejs.js" etc. It doesnt matter where the actual files are.

                      lets say you have couple of files in different folders and files:

                      For example:
                      http://www.yoursite.tld/index.php and http://www.yoursite.tld/somedir/someotherdir/script.php you would still have the same href and they will work no matter where the actual script is used.

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

                      In the php side, offcourse declaring the SITE_ROOT is more than recommended but in html site you only have to use / character at the beginning to tell its looked from the root of the site.

                        it works now on server and on localhost when i use
                        define("SITE_ROOT","http://".$_SERVER['HTTP_HOST']);

                        thanks for help guys

                          thanks but anyway,
                          this works in html
                          but when i use

                          <?php require_once("/includes_html/head.php"); ?> it won't work:
                          Warning: require_once(/includes_html/head.php) [function.require-once]: failed to open stream:

                            Yes, HTML and PHP are two separate languages. What works for HTML will not always work for PHP and vice versa. With that include line, PHP is literally looking for the filesystem folder "/includes_html" which doesn't exist. So for PHP includes, you should use:

                            <?php
                            require_once($_SERVER['DOCUMENT_ROOT'] . '/includes_html/head.php');

                            For HTML, if you know the direct path from the document root, you can just say:

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

                            For example in my index.php which is in root directory it will look like this:
                            <link rel="stylesheet" type="text/css" href="css/main.css" />

                            and in subfolder files it will look like this:
                            <link rel="stylesheet" type="text/css" href="../css/main.css" />

                            No, the <link> tags will look the same because HTML defines paths relative to the root of the website IF the href or src attribute starts with a "/" (i.e. if you don't specify a domain, it uses the one you're currently at to look for that resource).

                            For example, if in the root of your site you have:

                            <link rel="stylesheet" type="text/css" href="/styles/site.css" />
                            <script language="javascript" type="text/javascript" src="scripts/jQuery.js"></script>

                            HTML will look relative to the domain root for the stylesheet, and relative to the current document's path for the script. So if you have a subfolder (say "about") and in that index page you have the same exact code, the CSS would be included (since HTML will use the domain root as the base) but the jQuery include would fail because there is no subdirectory "scripts" in the about folder (which is where the current file resides).

                            With PHP, everything is relative to the current script being executed. Meaning that if you include a file and use a relative path to include another file, you're in for some snags as the paths will get murky. This is why it's good in PHP to designate a root variable to refer to or use a more absolute relative path. A more absolute relative path would be something like:

                            <?php
                            
                            $thisFile = dirname(dirname(__FILE__));

                            Then when you do an include you can do:

                            <?php
                            
                            $thisFile = dirname(dirname(__FILE__));
                            
                            include($thisFile.'/some_other_dir/other-file.php');

                            So it's still "relative" but it's more absolute. It's relative to the current document, but gives you an absolute designation as to where it is relative to that document. Much better than "../../../../../../" stuff. Of course you could always say:

                            <?php
                            define('ROOT', realpath(dirname(__FILE__)));

                            in some file in the web-root that is included with every other file and voila!!

                            And there is also the last option which is to put all your includes into one folder, and add that folder to the path so you can just do includes by name, rather than defining paths 😉

                              thanks for detailed explanation. I understand now.🙂

                                Write a Reply...