The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> PHP Includes which contain includes
kohenkatz
post Aug 24 2008, 02:13 AM
Post #1





Group: Members
Posts: 2
Joined: 24-August 08
Member No.: 6,489



I am using php includes to include a set of functions in all my pages.
However, some of these functions contain includes as well.

example.com/index.php
which includes:
example.com/inc/functions.php
which in turn includes:
example.com/inc/dbcon.php

The problem is that PHP looks for the second include file relative to the first file. So I would need to put "include('inc/dbcon.php');" in the second file. However, that makes it that I can no longer use these functions from any other directory.

i.e.
example.com/subf/index.php
which includes:
example.com/inc/functions.php
which in turn includes:
example.com/inc/dbcon.php

will not work because it wants to do "include('../inc/dbcon.php');".

What can I do about this?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Tom H.
post Aug 24 2008, 07:13 AM
Post #2


Advanced Member
****

Group: Members
Posts: 103
Joined: 24-August 06
Member No.: 14



I had similar difficulty, but had success calling the nested include like this:
CODE

<?php include('./path/from/web-root/inc/nested.incl'); ?>

I can't find any documentation to support it, but it seems like the leading dot-slash indicates the web server document root.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
kohenkatz
post Aug 24 2008, 01:08 PM
Post #3





Group: Members
Posts: 2
Joined: 24-August 08
Member No.: 6,489



QUOTE(Tom H. @ Aug 24 2008, 07:13 AM) *

I had similar difficulty, but had success calling the nested include like this:
CODE

<?php include('./path/from/web-root/inc/nested.incl'); ?>

I can't find any documentation to support it, but it seems like the leading dot-slash indicates the web server document root.


no. this would not work for me. The dot does not mean the web root, it means the current directory. In your case, the current directory happens to also be the web root.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Aug 24 2008, 01:42 PM
Post #4


Don't like donuts. Don't do MySpace.
********

Group: WDG Moderators
Posts: 7,207
Joined: 9-August 06
Member No.: 6



What about using the fully qualified URL (http://...)? I also think a system path works. That is, something like so:

CODE
<?php include('/home/you/domains/example.com/inc/nested.incl'); ?>



--------------------
“Never go to excess, but let moderation be your guide.”
– Cicero
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Aug 25 2008, 03:24 AM
Post #5


Jocular coder
*******

Group: Members
Posts: 1,431
Joined: 31-August 06
Member No.: 43



I think the "proper" way is to have all the include files somewhere outside the document tree, then they can't be accidentally dished up by the web server.

But if not, use $_SERVER["DOCUMENT_ROOT"] to get to the document root.

You do have a phpinfo() page you use, don't you?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 21st November 2009 - 07:03 AM