Welcome to WebmasterWorld Guest from 54.162.157.249

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Correct file path for php includes: relative, absolute, other?

     
1:00 am on Mar 26, 2008 (gmt 0)

5+ Year Member



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!

2:20 am on Mar 26, 2008 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.

2:27 am on Mar 26, 2008 (gmt 0)

WebmasterWorld Senior Member marcia is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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/.

11:37 am on Mar 26, 2008 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Typo:

Use URLs that being with /

should be:

Use URLs that begin with /

11:50 am on Mar 26, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



If you use absolute url's for includes then you need to use the file system path, not the web path. As the include is carried out by the server not the browser.

Example -


include('/var/www/domains/example.com/htdocs/include/some_file.php');

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]

11:53 am on Mar 26, 2008 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



I sometimes do set the include path using PHP_Value in the .htaccess file.
1:18 pm on Mar 26, 2008 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Another option from our PHP Forum Library [webmasterworld.com] ...
Control your included files [webmasterworld.com]
7:00 pm on Mar 26, 2008 (gmt 0)

5+ Year Member



Alright, thanks for all the extremely helpful information guys. That should set me strait. Take care!
 

Featured Threads

Hot Threads This Week

Hot Threads This Month