Problem using include() in subdirectories

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • seegoon
    New Member
    • Oct 2009
    • 31

    Problem using include() in subdirectories

    Hi there. You guys are going to save my life.

    I've recently started changing a site from plain ol' html to php, so that I can use includes for headers, footers and so on, and also so that in the future dynamically creating pages won't be a problem.

    I have includes working a-okay at the moment for files in the root directory, i.e. in http://buddingpress.co.uk/sitemap.php I have three functional includes, all basically using the code:
    Code:
    <?php include("includes/footer.php"); ?>
    The problem is that I want to use includes for pages in subdirectories, i.e. http://buddingpress.co.uk/contact/index.php – but the darn thing just doesn't work the same. Inserting a slash, as you might with an image or href or whatever, doesn't seem to work. It actually sends the whole thing berserk.

    So is there a nice, attractive fix? Or do I have to just direct the includes by using an entire url, including the http://? I'm sure this is a php basic that I'm just ignorant of.

    Thanks for reading!
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the best approach is usually to use absolute paths (like "/var/www/index.php")
    if you define a constant in your base file, you can use this constant to extend any relative paths to absolute paths.
    Code:
    // if this is called in a file in the base directory
    	# root directory of project
    	define("BASE_DIR", realpath(dirname(__FILE__))); 
    
    // use as
    include BASE_DIR . "includes/footer.php";
    note: personally I define this constant in a conf script, that is called in every page.
    .htaccess (root folder)
    Code:
    php_value include_path ".:/path/where/load.php/resides"
    any PHP page
    Code:
    <?php
    // a configuration script that calls all the settings
    // the "include_path" setting makes sure, this works from every folder
    require "load.php";
    
    // rest of the code
    directory configuration file (called in load.php)
    Code:
    // the root folder is 2 directories above this file’s folder
    	# root directory of project
    	define("BASE_DIR", realpath(dirname(__FILE__)) . '/../../');
    example of a load.php
    Code:
    <?php
    /* load settings & constants */
    	
    	# directroy constants
    	require 'conf.dir.php';
    	
    	# common flags
    	require CONF_DIR . 'conf.flags.php'; // CONF_DIR has been defined in "conf.dir.php"
    	
    	# project settings
    	require CONF_DIR . 'conf.settings.php';
    	
    	# DB constants
    	require CONF_DIR . 'conf.db.php';
    	
    /* autoload functions */
    	
    	# loading standard classes via __autoload()
    	require CONF_DIR . 'autoloads.php';
    	spl_autoload_register('AL_interface');
    	spl_autoload_register('AL_swift4');
    	spl_autoload_register('AL_main');
    	
    	# load all exception classes too
    	require LIB_DIR  . 'classes.exceptions.php';
    	
    /* error/exception handling */
    	
    	require LIB_DIR . 'main.errorhandler.php';
    	require LIB_DIR . 'main.exceptionhandler.php';
    ?>

    Comment

    • seegoon
      New Member
      • Oct 2009
      • 31

      #3
      Wow - there's certainly a lot more for me to learn before I can go messing around with code like this. For the time being, I've just told it to track backwards, using ../ in the address. I just need to spend a bit more time reading up on my basics; I only started thinking about using it yesterday. There will be plenty of roadbumps along the way. Thanks for your answers anyway, though!

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by seegoon
        There will be plenty of roadbumps along the way.
        things you should be looking for when getting an error:
        • carefully read the error description, it usually provides some kind of solution
        • look for missing ";", ")" or "}"
        • look for typos in variable names or parameter values

        Comment

        Working...