Msg#: 3610645 posted 1:00 am on Mar 26, 2008 (gmt 0)
Hello, I have 3 folders including my root folder - they are: root folder, content folder, and includes folder. The links in my nav and footer php include look like ../index.php. This link works for all the files in the content folder but not the root folder. And if I make the link like index.php it works for the opposite and not for the others.
How should I be pointing my links to my files within my php includes? How do you guys do it? I have found some answers but they all seem a little unnecessary - they are all these small to large php scripts. Well there has to be some way to just point at file, regardless of where it sits, and be done with it, right? Thanks again!
Msg#: 3610645 posted 2:20 am on Mar 26, 2008 (gmt 0)
Think it through like this. The include file hasn't got a URL of it's own. It only has a folder location on a hard drive.
The content of the include file will be included internally by the server, to be a part of some other page, at the URL for that other page. Only then will the browser be able to resolve a URL from that data in the link.
Use URLs that being with / to show the full path tto the other content. It will then always resolve correctly. URLs in links are otherwise resolved by the browser using the URL for the currently viewed page as a base.
Msg#: 3610645 posted 2:27 am on Mar 26, 2008 (gmt 0)
For using includes on pages in /directories/ the easiest way when includes are in their own directory called /includes/ is to call the includes for the pages like this:
<?php include("../includes/thecode.php"); ?>
For pages in the root, this is what I use that works:
<?php include ("includes/thecode.php"); ?>
For using absolute paths I believe you'd have to use the actual Apache paths (not the same as the URI). It's easier to just use relative links, but they can't be done the same way for pages in the root as they are for pages in /directories/.
Will include the same file from any location, but this is the file system location. As the web root, /, would be '/var/www/domains/example.com/htdocs/' So if you try - include('/include/some_file.php');
You will probably get an error and if you dont get an error then you are unlucky as you are not including the same file as you thought.
On some of the sites where I have subdomains set up I generally use absolute url's for includes. As the subdomains sit on the same server, however have different file system paths to the web root. So instead of copying the code into all of the subdomains I can use 1 set of code across all of them. This does rely on settings in php.ini that are not always available on all servers, so this may or may not work for you anyway.
<edit> Same thing as Marcia said above.
[edited by: PHP_Chimp at 11:51 am (utc) on Mar. 26, 2008]