Why don't links to include files work when you link relative to site root but do when you link relative to file? Is there a way round this?

<?php require_once('/head.php');?>

    Some relative links works:

    include_once("../file.php");

    PHP prolly recognized the slash root as root of the filesystem and not the web. If you do this:

    define(_DOCROOT_, "/var/www/html/project");
    
    include_once(_DOCROOT_ . "/file.php");
    

    This works.

      Found this which works perfectly.

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

        That works well when your project is in the document root. Just be aware that if you have several different sites and break them down into subdirectories (I have about 10 different projects on my machine) you'll want to do something like:

        define(DOCROOT, $_SERVER['DOCUMENT_ROOT'] . "/project5");

        But I didn't know about that particular server variable -- learn something new every day! 😉

          That's OK. I never work on subdomains. Thanks for your help
          Andy

            Write a Reply...