473,386 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

includes and paths and organization

Ron
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


Sep 25 '07 #1
4 1807
In our last episode, <2007092500072816807-PostInGroups@wherevercom>, the
lovely and talented Ron broadcast on comp.lang.php:
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?
Sep 25 '07 #2
Ron wrote:
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
Sep 25 '07 #3
Erwin Moller <Si******************************************@spam yourself.comwrites:

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/
Sep 26 '07 #4
Ron
On 2007-09-25 22:03:51 -0700, Jerry Stuckle <js*******@attglobal.netsaid:
>>

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

Sep 26 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Tim Tyler | last post by:
A fairly simple question: I have a library A which depends on library B and C. Currently I have: <?php include("A.inc.php"); include("B.inc.php"); include("C.inc.php");
4
by: Ben | last post by:
This is probably a very simple question (I hope), but my usually great web searching abilities have not turned up the answer I am looking for. I have a php page that includes a file for example...
2
by: Mike | last post by:
Hello, I'm working on a new site that has a nearly empty index file (variable location) that includes a template file (set location) which in turn includes two data files (variable location...
7
by: jason | last post by:
I am getting twisted by the possibility that my virtual includes which currently work great on non-domain remote IP will crash if I purchase a domain and point it to one of my designated...
6
by: ceyhun | last post by:
When I use an include file like this <!--#include file="..\..\cfig.asp"--> <!--#include file="..\..\clsOOPModule.asp"--> my server gives an error of : Active Server Pages error 'ASP 0131' ...
3
by: SuryaPrakash Patel via SQLMonster.com | last post by:
Hello, There are three tables: OS-GroupOFCompanies (Table1) GoC_GroupOFCompaniesID (PK) OS-Organization (Table 2) Org_OrganizationID (PK)
1
by: paddy | last post by:
I keep digging through tut after tut and I find lots and lots of C++ logic, but I can't find any straight forward plain and simple info on the whole linking and library process. Can anyone...
3
by: Jeff | last post by:
I have an asp page. Now I know this is sortof OT, but I am not sure of the forum to get an answer. I have an include on this asp page <!-- #include file=\forum\includes\consts-inc.asp --> ...
6
by: MaiyaHolliday | last post by:
Hello, I've recently installed apache on a new computer, and cannot figure out why my site will not process any includes. (it was working on my old one) There are no errors on the page such as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.