Connecting Tech Pros Worldwide Forums | Help | Site Map

includes and paths and organization

Ron
Guest
 
Posts: n/a
#1: Sep 25 '07
Hi all,

Trying to work out a few problems in using php on my site. Partially
this is a html question. I was reading a lot of the posts and it seems
that some of the includes people are using are very complicated because
of sessions or id's and variables I don't really know. So...

Right now i use php for includes and getting some lists I make using
mySQL and phpmyadmin and can get those out without error. For the most
part a lot of my files were in the main directory with my index.php
like header.php and footer.php but I just realized when I use these
header and footer files from a different directory the paths are wrong.
I was doing the includes like:
<?php include("header.php"); ?Links are good
<?php include("../header.php"); ?Links are bad
<?php include("../../header.php"); ?Links are bad
The linked file in the header file would be at the same level as index
but now as I have added pages I have made folders to keep things
organized. My goal is to call my header or footer file from any level
folder from an includes folder.
<?php include("includes/header.php"); ?being called from collies in
mysite/pets/dogs or whatever.

My solution is to make the link the full path with http://mysite.com
and so on so regardless of where the header is called from the link is
good. I always thought that relative paths are used within a site and
full paths to link outside. My solution will work but I think there is
a better way.

So should I have my include files at the same level as my index file?
or in a folder?
Use full path?
And should these include files use a .inc extension instead of php?

Sorry this is a bit long for such a simple topic but I have to get
organized. :)

thx..ron







Lars Eighner
Guest
 
Posts: n/a
#2: Sep 25 '07

re: includes and paths and organization


In our last episode, <2007092500072816807-PostInGroups@wherevercom>, the
lovely and talented Ron broadcast on comp.lang.php:
Quote:
My solution is to make the link the full path with http://mysite.com
and so on so regardless of where the header is called from the link is
good. I always thought that relative paths are used within a site and
full paths to link outside. My solution will work but I think there is
a better way.
There is. Write a php function to calculate relative paths.

--
Lars Eighner <http://larseighner.com/ <http://myspace.com/larseighner>
Countdown: 483 days to go.
What do you do when you're debranded?
Erwin Moller
Guest
 
Posts: n/a
#3: Sep 25 '07

re: includes and paths and organization


Ron wrote:
Quote:
Hi all,
>
Trying to work out a few problems in using php on my site. Partially
this is a html question. I was reading a lot of the posts and it seems
that some of the includes people are using are very complicated because
of sessions or id's and variables I don't really know. So...
>
Right now i use php for includes and getting some lists I make using
mySQL and phpmyadmin and can get those out without error. For the most
part a lot of my files were in the main directory with my index.php like
header.php and footer.php but I just realized when I use these header
and footer files from a different directory the paths are wrong. I was
doing the includes like:
<?php include("header.php"); ?Links are good
<?php include("../header.php"); ?Links are bad
<?php include("../../header.php"); ?Links are bad
The linked file in the header file would be at the same level as index
but now as I have added pages I have made folders to keep things
organized. My goal is to call my header or footer file from any level
folder from an includes folder.
<?php include("includes/header.php"); ?being called from collies in
mysite/pets/dogs or whatever.
>
My solution is to make the link the full path with http://mysite.com and
so on so regardless of where the header is called from the link is good.
I always thought that relative paths are used within a site and full
paths to link outside. My solution will work but I think there is a
better way.
>
So should I have my include files at the same level as my index file? or
in a folder?
Use full path?
And should these include files use a .inc extension instead of php?
>
Sorry this is a bit long for such a simple topic but I have to get
organized. :)
>
thx..ron
>
>
Hi Ron,

In addition to what Jerry and Lars wrote:

I prefer making one or two directories where I store my includes, and
simply add them to the include path. This makes things easy to manage.

You can use ini_set, eg:
$newIncludePath =
get_include_path().PATH_SEPARATOR.'/home/bla/public_html/includes';

ini_set("include_path",$newIncludePath);


You only need 1 file you include everywhere in all your scripts that
contains this, and you are fine.
If you ever move your directorytree, just adjust the path.

Now you can simply include everything that is stored under
/home/bla/public_html/includes, and also its subdirectories (if you name
them during your include).


Regards,
Erwin Moller
John Murtari
Guest
 
Posts: n/a
#4: Sep 26 '07

re: includes and paths and organization


Erwin Moller <Since_humans_read_this_I_am_spammed_too_much@spam yourself.comwrites:

Quote:
I prefer making one or two directories where I store my includes, and
simply add them to the include path. This makes things easy to manage.
>
You can use ini_set, eg:
$newIncludePath =
get_include_path().PATH_SEPARATOR.'/home/bla/public_html/includes';
>
ini_set("include_path",$newIncludePath);
>
>
You only need 1 file you include everywhere in all your scripts that
contains this, and you are fine.
If you ever move your directorytree, just adjust the path.
>
Now you can simply include everything that is stored under
/home/bla/public_html/includes, and also its subdirectories (if you
name them during your include).
>
I would vote for Erwin's solution since it will give you
much more flexibility in include location. Readup on "include_path"
in on the php web site; also, some people may now know that you can use
relative paths in the include_path directive, so this is okay:

include_path = ".:lib:../lib:../../lib"

NOTE, the "." is added first since the normal PHP use of the include_path
is to search the include_path FIRST, if the file is not found along those
paths, look in the default dir (which is usually what you might not expect).

Hope this helps.

--
John
__________________________________________________ _________________
John Murtari Software Workshop Inc.
jmurtari@following domain 315.635-1968(x-211) "TheBook.Com" (TM)
http://thebook.com/
Ron
Guest
 
Posts: n/a
#5: Sep 26 '07

re: includes and paths and organization


On 2007-09-25 22:03:51 -0700, Jerry Stuckle <jstucklex@attglobal.netsaid:
Quote:
Quote:
>>
>
Nope, the beautiful thing is it works on any server where PHP is loaded
as a non-cgi.
I started a file for all my little snippets. The hard thing is not
knowing where to look in the manual sometimes.

I use my iMac as a test sever as it comes loaded with php4. The path
reurned isn't really where you think it is so the include fails. Minor
issue as it works great in the real world.

Thanks again,
Ron

Closed Thread