Reply
Require/include question..
Old 05-05-2007, 12:24 PM Require/include question..
cbeaudin's Avatar
Super Talker

Posts: 126
Name: Clayton Beaudin
Location: Proud to be Canadian
Is there a simple way to require/include files that have links relative to the folder they are in?

ie. If i use
PHP Code:
require('nav.php'); 
in a file that is in the root directory and has a link to contact.php in the same directory the link would look like this.
HTML Code:
<a href='contact.php'></a>
Now if i want to have a file in a different directory (not the root folder) ie. /root/scripts/forum/index.php

is there a way to require(../../../nav.php) and have the links in that file relative to the folder that nav.php is in.

ie, the contact link should now look like this
HTML Code:
<a href='../../../contact.php'></a>
I hope i explained that well enough, i had i hard time trying to explain it in words, sorry if it doesnt make sence.
cbeaudin is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 05-05-2007, 01:35 PM Re: Require/include question..
tripy's Avatar
Fetchez la vache!

Posts: 2,057
Name: Thierry
Location: In the void
What I like to use in this case is relative path, but not from the current path, but from the site root.

Just a reminder:
Quote:
./a.html => the file a.html in the current folder
../a.html => the file a.html in the parent folder
/a.html => the file a.html in the root of your site
So, when you prepare your site structure (because, you have planned it, right ???) you know which page is going to be where. So in your include, you can define them with an "/dir/subdir/file" rather than "../../../subdir/file".

What you need to do, then, to locate your include files, is to base your include on the PHP generated variable.
Particulary on $_SERVER['DOCUMENT_ROOT'] which give the path to the root of your web site, on the perspective of the server.

This is what I do in my prepended script.
A prepended script is a script run before every page access. I use it to initiate db connection, and include object oriented classes for object stored in session.
PHP Code:
define('DOC_ROOT',$_SERVER['DOCUMENT_ROOT']);

require_once(
DOC_ROOT."/libs/db/db.php");
require_once(
DOC_ROOT."/classes/init.php");
require_once(
DOC_ROOT."/logger/logger.php");

/*
* A buch of odd stuff, like sql injection prevention and such...
*/
session_start()

require_once(
DOC_ROOT."/db/init.php"); 
Now, every time I need to include something, from anywhere in the site, I simply use the DOC_ROOT (way shorter than $_SERVER['DOCUMENT_ROOT']) define I've set in my prepend file.
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 05-05-2007, 03:15 PM Re: Require/include question..
cbeaudin's Avatar
Super Talker

Posts: 126
Name: Clayton Beaudin
Location: Proud to be Canadian
Thank you, Very helpful.

I know i did it a weird way in the beginning for a few reasons. The way you do it should work very nice.
cbeaudin is offline
Reply With Quote
View Public Profile
 
Old 05-05-2007, 08:14 PM Re: Require/include question..
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 6,173
Name: Dan
Location: Swindon
Or... couldnt you just put the full file path?... :|

for my navi.php and that i make all file paths <? echo "$core" ?>/whatever.php and then on everypage i have a config.php included which has $core = http://whatever i find it makes it aloet easier, dont have to worry about if i move it form domain to domain or fodler etc, i can just change ne thing in the config file...

~Dan

Btw is require and include the same or is there any differances?
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)

Last edited by dansgalaxy : 05-05-2007 at 08:17 PM.
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 05-05-2007, 09:00 PM Re: Require/include question..
tripy's Avatar
Fetchez la vache!

Posts: 2,057
Name: Thierry
Location: In the void
There is a slight difference between them.

Include only trigger a warning if the file is not found, but the scripts carry on.
Require throws an error, and breaks the script.
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 05-06-2007, 07:28 AM Re: Require/include question..
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 6,173
Name: Dan
Location: Swindon
So wouldn include be the preferd method?... why would anyone use require?..
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 05-06-2007, 07:37 AM Re: Require/include question..
tripy's Avatar
Fetchez la vache!

Posts: 2,057
Name: Thierry
Location: In the void
Because there are case where I don't want my app to continue when there are errors.
And I don't want to digg in the log file to see why something don't work.

Quote:
Or... couldnt you just put the full file path?
I don't do this for 1 simple reason.

I develop my apps on a local server.
When I publish the files on the production server, the path and many other things are different.
I use a revision management system to keep track of my updates (svn, if you're curious), and in this view, I want to have the same files that can adapt themselve upon both servers.

So, I put variables in the apache config (via the setEnv directive) and many of my modules adapt themself upon those variables.
Especially on the LOCATION variable, which state if the site is dev, preprod or production, and switch to the db dsn accordingly.
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 05-06-2007, 08:09 AM Re: Require/include question..
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 6,173
Name: Dan
Location: Swindon
yea, but would this work (in a more difficult way) to me doing <? echo "$core" ?>/file/path ?

because then u just change one variable, and every file path in the iste changes instantly!?

Unless there is soemthing i am missing,
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 05-06-2007, 12:18 PM Re: Require/include question..
tripy's Avatar
Fetchez la vache!

Posts: 2,057
Name: Thierry
Location: In the void
Quote:
because then u just change one variable, and every file path in the iste changes instantly!?
Yes, of course it works, but it means you must use different files on your production server and your dev server, and that's a scheme I want to avoid at any costs.
When you work alone, no problem.
When you are 5 developpers working together, one make an update, the other overwrite the file, and bam, nothing works anymore.

1 file, with 1 code, and the adaptation is done outside PHP, this way PHP only hold the logic, and the details are setup outside of php.
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 05-06-2007, 01:03 PM Re: Require/include question..
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 6,173
Name: Dan
Location: Swindon
Im not going to argue any more XD.

Dan
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 05-06-2007, 01:33 PM Re: Require/include question..
tripy's Avatar
Fetchez la vache!

Posts: 2,057
Name: Thierry
Location: In the void
I won !!!

:-D)))

Seriously, I was true on my reasons, I have been confronted to problems because of the production files/dev files sepraration, and I used this technique for a few years now, and it avoided me several problems.

No hard feelings, I hope, Dan.
__________________
Listen to the ducky: "This is awesome!!!"


Last edited by tripy : 05-06-2007 at 02:07 PM.
tripy is offline
Reply With Quote
View Public Profile
 
Old 05-06-2007, 01:39 PM Re: Require/include question..
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 6,173
Name: Dan
Location: Swindon
Are you kidding?

Im going to form a whole hate campaign against you now!,
You [Insert REALLY REALLY REALLY REALLY REALLY REALLY insulting words here] [Add millions of ! here]



Im crying now over all the horible mean stuff you said!!!


DAN
(annouying webmasters since 2005)
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)

Last edited by dansgalaxy : 05-06-2007 at 01:41 PM.
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 05-06-2007, 02:08 PM Re: Require/include question..
tripy's Avatar
Fetchez la vache!

Posts: 2,057
Name: Thierry
Location: In the void

O god... And I let my unedited post for almost 1 hour...
Shame on me....

__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 05-06-2007, 05:01 PM Re: Require/include question..
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 6,173
Name: Dan
Location: Swindon
Lol, all i can say is my methods suit me.

all i need is for the rest of he internet and the world to make eveything fit my perfect methods for everything. then we would all be happy... where did i put that bobble hat?... my feet are cold... XD

just some random crap Xd...
Dan
(wasting server space since 2002)
__________________
Personal UK Webhosting
Get 25% of ANY shared package for life ~ Promo: webmaster-talk (only for members!)
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to Require/include question..
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off