I recently started using more and more php include () for several small sites with no directories and am now working on a large site with several directories and sub-directories.
I also use php include () for the navigation where my problem starts.
With a small site and no sub-directories I can use a relative path and preview the whole site locally using the testing server, but with directories and sub-directories, I have to use an absolute path to get the links working, but then I can’t check my site locally on my computer (without uploading at first), because the browser tries to load absolute paths from the server and not from my testing server (Inetpub\wwwroot\).
Is there any work-around for this (Win XP Pro, DW MX 2004)?
How do you handle this?
include ("../included_files/include.php"); //outside the root dir
include ("../../included_files/include.php"); // two folders up outside the root...
include ("http://www.include.com/index.php"); // remote files
include ("inc/my_included_files.php"); // sub directories
include ("file_name.php"); // same directory...
i would make sure that the directory structure is the same on the server as it is on your local machine, that way it should work locally and on the server.
You’re of course right, I don’t know what I was thinking when I posted this:blush:, maybe I should take a break…
But there’s still a problem when I use relative paths within the included file (the navigation) which is called from different directories or sub-directories.
You need to remember that any paths in the include file are relative to the script that calls the include and NOT the include its self.
You have to be aware of that when structuring your site.
An include almost literally pastes the entire content of its code into the calling script.
[list]
[]root/
[]root/dir-a/
[]root/dir-a/sub-1/
[]root/dir-a/sub-2/
[]root/dir-b/
[]etc.
[/list]My included files are all in “root/include/” and the my included navigation looks like this
The files "file-a.php", "file-b.php" and "file-c.php" are all in "root/"
I would like to call the included navigation from any of the above directories.
If I have a file “myfile.php” in “root/” and call the included navigation, then the included navigation works, if I click on the link “file-a”.
If I have a file “myfile.php” in “root/dir-a/sub-1/” and call the included navigation, then the included navigation does not work, if I click on the link “file-a”, because the path should be “…/…/file-a.php” and not just “file-a.php”.
This means that I have to use absolute paths for the included navigation. Or am I totally wrong here?
And then I have problems previewing my site locally, because the browser connects to the real server and not the testing server.
Hi,
The solution is pretty simple. I used it on some of my sites which have files nested inside directories, and have subnavs, etc.
FIRST:
set up a variable. I call mine $path2root. I put this variable at the top of everypage on the site. Ofcourse this must be before the include script.
SECOND:
here’s how I point to my file with the navigation in it which exists in root/includes
First put this at the top of every page, before the include to the nav file, of course the values would change depending where the file is located. So this file is in root/dir-a/sub-1
$path2root = "../..";
<?php include "$path2root/includes/nav.php" ;?>
and in the nav.php file, the links for my nav would look like this:
And actually I use another $path2root for other things and use $path2nav. Why? well if you are on a page say index.php which is in your root dir. and you have the nav.php included. The user clicks to go to contact.php which also exists on the root dir, the code above goes to the link: /contact.php
Do you see the problem? the slash shouldn’t be there, so using $path2nav on everypage for navigation I can do this: (this is for files on the root)
This is just my personal preference, however. Just as any method would, it does have its obvious advantages and disadvantages. Feel free to use however you’d like - at your own risk, of course.
I have not been using many includes in the past, but I’ll give all solutions here a try and see what works best for me. Everything is better than using absolute paths.
add this to your index.php file
<div id=“content”
<?
if (nav < ‘1’){
include(“includes/main.php”)}
?>
</div>
or
<div id"content">
<?
if (nav == '1"){
include(“$path2includes/contact.php”)}
?>
</div>
and so on…
This way everything spurs off of your index.php and root and you will not have to wory about absolute paths…This also allow for you to change the layout of the site just by changing the CSS or index.php page and
The dirname command gets the parent directory of the one specified (aka the document root) and then I can put in my php directory and the filename of the include file.
Something that I do is create a variable called $page_root_path and include that on every page at the top. I then append all links, includes, references and what not using that variable. Similar to:
I use an absolute path defined seperately now too. That way it will work if you move a directory to another location or nest it down further without having to change anything (except maybe links to the page ;)).