The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

3 Pages V  1 2 3 >  
Reply to this topicStart new topic
> PHP include function
Dr Z
post Mar 14 2007, 07:35 PM
Post #1


Advanced Member
****

Group: Members
Posts: 221
Joined: 23-August 06
Member No.: 11



I am no expert in PHP; I have dabbled in it few times through ou the years, but, each time, not long enough to get anywhere.

I am trying to switch from SSI include to PHP include, again for off-line testing.

The point where I need help is the path to the included file.

I have a file menu.inc, which resides in the root file. When including in file that already resides in the root file, then
<?php include 'menu.inc'; ?> works (I have tested it last night)

Now, I am trying to include the same file in a file that does not reside in the root directory, but a sub-sub directory. when I use
<?php include '/menu.inc'; ?>, it does not work.

I seems that it is something to do with relative path.

Any help will be appreciated.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Effovex
post Mar 14 2007, 10:56 PM
Post #2


Serious Coder
*****

Group: Members
Posts: 251
Joined: 6-January 07
From: Sherbrooke, Qc, Canada
Member No.: 1,477



"/menu.inc" looks in the root directory of the webserver, IIRC.
"menu.inc" looks in the root directory, then in the directory of the currently executing script (provided your include path has "." in it)
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dr Z
post Mar 14 2007, 11:14 PM
Post #3


Advanced Member
****

Group: Members
Posts: 221
Joined: 23-August 06
Member No.: 11



QUOTE(Effovex @ Mar 14 2007, 07:56 PM) *

"/menu.inc" looks in the root directory of the webserver, IIRC.
"menu.inc" looks in the root directory, then in the directory of the currently executing script (provided your include path has "." in it)


Could you please elaborate, I am not sure if I understand it, especially about "provided your include path has "." in it"

How should I refer to the file to be included, when the main page is not in the root directory, but the file to be inculeded is in the root directory, or for all practical purposes, in any other directory other then the that of the main file.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Peter1968
post Mar 14 2007, 11:42 PM
Post #4


Serious Coder
*****

Group: Members
Posts: 448
Joined: 23-September 06
Member No.: 213



It's the include_path variable in PHP's core setup.

E.g, mine is ".:/usr/lib/php:/usr/local/lib/php"

So, to address a file that's somewhere else in your hierarchy other than where the script is located, you'd need to do the full directory path like /home/drz/public_html/yourstuff or whatever your home directory is according to your server.

That's my understanding of it.

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dr Z
post Mar 15 2007, 09:27 PM
Post #5


Advanced Member
****

Group: Members
Posts: 221
Joined: 23-August 06
Member No.: 11



QUOTE(Peter1968 @ Mar 14 2007, 08:42 PM) *

It's the include_path variable in PHP's core setup.

E.g, mine is ".:/usr/lib/php:/usr/local/lib/php"

So, to address a file that's somewhere else in your hierarchy other than where the script is located, you'd need to do the full directory path like /home/drz/public_html/yourstuff or whatever your home directory is according to your server.

That's my understanding of it.


I am still confused, I must admit. I am trying to set up the PHP in my computer for off-line testing, not on a server. Additionally, the referenced file is in the root directoy. then what would the absolute path be?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Mar 15 2007, 10:10 PM
Post #6


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
Joined: 9-August 06
Member No.: 6



Use ($_SERVER['DOCUMENT_ROOT'] for the server path to your document root.

<?php
include($_SERVER['DOCUMENT_ROOT'].'/menu.inc');
?>

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dr Z
post Mar 16 2007, 05:28 PM
Post #7


Advanced Member
****

Group: Members
Posts: 221
Joined: 23-August 06
Member No.: 11



It did not work, but have feeling that this due to the fact I am using vhosts. The root directory is the root directory of one of the vhosts, not the main root directory of Apache.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Mar 16 2007, 05:32 PM
Post #8


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
Joined: 9-August 06
Member No.: 6



You don't want the document root. You want the full path.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Peter1968
post Mar 16 2007, 06:50 PM
Post #9


Serious Coder
*****

Group: Members
Posts: 448
Joined: 23-September 06
Member No.: 213



Someone correct me but ($_SERVER['whatever']) only works if register_globals is set to on, right? I know it is by default for PHP < 4.3.0 but most hosts nowadays have either moved to a higher version where it's disabled, or have disabled it altogether in the php.ini.

I think you may have to set it to on in your own php.ini if you're running a LAMP setup like I think you are.

If I'm wrong, do tell me, as I'm going from memory here messing with PHP and PostNuke for years.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Mar 16 2007, 07:23 PM
Post #10


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



QUOTE(Peter1968 @ Mar 17 2007, 12:50 AM) *

Someone correct me but ($_SERVER['whatever']) only works if register_globals is set to on, right?

Not AFAIK, http://www.php.net/manual/en/reserved.vari...ariables.server says

This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. You don't need to do a global $_SERVER; to access it within functions or methods, as you do with $HTTP_SERVER_VARS.

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dr Z
post Mar 23 2007, 10:56 PM
Post #11


Advanced Member
****

Group: Members
Posts: 221
Joined: 23-August 06
Member No.: 11



Folks, sorry for the time delay, but i did not have Internet access for a while due to a comedy of errors. (It took one phone call to my ISP to resolve it, but the the problem was the luxury of time to make that call.)
Based on the the few replies to this post, I went back to my notes. (Every time I have had the luxury to experiment with PHP, I make notes to myself). Under PHP include function, I found this note:

"Only absolute path?????????"

Perhaps, I should re-word my original question:
PHP include function allows relative paths or only absolute paths are allowed?

If only absolute paths are allowed, it defeats the purpose of off-line testing (especially using vhosts) since the absolute path in my computer will be different than that of the server.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Mar 24 2007, 02:52 AM
Post #12


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



Of course you can use relative paths for include. You may have decided not to because you are writing them in a bit of program that gets included in files in different directories.

I don't understand though, quite how you are going to execute the php files without using a server. Do you have some sort of browser function that will run the php interpreter each time you access a local php file???
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Mar 24 2007, 01:22 PM
Post #13


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
Joined: 9-August 06
Member No.: 6



What we've been trying to say is that include() isn't aware of the document root and you can't mimic <!--#include virtual="/..." --> straight off the bat. You have to get the path to the document root in some other way. If you use PHP to get that the include should work on any server.

Obviously "server/path/to/documnt root/path/to/inc.inc" won't work on other servers. But "[PHP trick to get the server path to the document root]/path/to/inc.inc" should.

Worth a read: http://se2.php.net/include/
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dr Z
post Mar 24 2007, 05:35 PM
Post #14


Advanced Member
****

Group: Members
Posts: 221
Joined: 23-August 06
Member No.: 11



QUOTE(pandy @ Mar 24 2007, 10:22 AM) *

Obviously "server/path/to/documnt root/path/to/inc.inc" won't work on other servers. But "[PHP trick to get the server path to the document root]/path/to/inc.inc" should.

Worth a read: http://se2.php.net/include/


I did look at that site many times. I cannot say I am understanding them totally. However, I understand the concept. Even with what ever trick this approach will not work with vhosts.

Let me try to explain:

The config file in my computer sets:
document root to "Apache directrory/httpd"
each vhost document root to "Apache directrory/vhosts/domain_name/httpd"
when the host_name of each vhost is entered into the address bar, the the root directory of that vhost is accessed.

Consequently, the document root of each vhost is different. Furthermore, on the actual server, the document root will be "Apache directrory/httpd" for any one of them, as will become independent domains (i.e. not a vhost). I have a sneaking suspicion that it could work in a dedicated virtual server, but I do not intend to have virtual server.

Any thoughts?

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dr Z
post Mar 24 2007, 05:37 PM
Post #15


Advanced Member
****

Group: Members
Posts: 221
Joined: 23-August 06
Member No.: 11



QUOTE(Brian Chandler @ Mar 23 2007, 11:52 PM) *

Of course you can use relative paths for include. You may have decided not to because you are writing them in a bit of program that gets included in files in different directories.

I don't understand though, quite how you are going to execute the php files without using a server. Do you have some sort of browser function that will run the php interpreter each time you access a local php file???

I have installed Apache and PHP under Windows in my computer.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Mar 24 2007, 06:34 PM
Post #16


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
Joined: 9-August 06
Member No.: 6



QUOTE(Dr Z @ Mar 24 2007, 11:35 PM) *

Consequently, the document root of each vhost is different. Furthermore, on the actual server, the document root will be "Apache directrory/httpd" for any one of them, as will become independent domains (i.e. not a vhost). I have a sneaking suspicion that it could work in a dedicated virtual server, but I do not intend to have virtual server.

That's why you shouldn't hardcode the path but use some PHP function to get the server path to the document root. Then it doesn't matter what it actually is, right? Doesn't $_SERVER["DOCUMENT_ROOT"] work on Windows? As I recall it does but I don't have PHP installed now so I can't try.
This is an Apaceh server and not IIS or something, right?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Mar 24 2007, 08:02 PM
Post #17


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



FWIW, a recurring problem for me when using Apache offline is that I keep each site project's files in its own sub directory, while in the online version the same files should stay in the web root. Not sure if finding the server path can help there. For example, the offline version's files might be located at "http://localhost/foo/" while the online version's files are at "http://foo.com/" (and not "http://foo.com/foo/" ).

Because of this I can't use absolute web paths, and therefore I can't include the same thing in any directory. One workaround might be to use something like this:

In the external script "functions.php"
CODE
<?php
function foo($dir_level)
{
    echo '<img src="'.$dir_level.'logo.jpg" alt="">';
}
?>


In files located in the web root directory:
CODE
<?php
include 'functions.php';
foo(''); // should return <img src="dog.jpg" alt="">
?>


In files located in child directories of the web root:
CODE
<?php
include '../functions.php';
foo('../'); // should return <img src="../dog.jpg" alt="">
?>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dr Z
post Mar 25 2007, 09:58 PM
Post #18


Advanced Member
****

Group: Members
Posts: 221
Joined: 23-August 06
Member No.: 11



QUOTE(Christian J @ Mar 24 2007, 05:02 PM) *

FWIW, a recurring problem for me when using Apache offline is that I keep each site project's files in its own sub directory, while in the online version the same files should stay in the web root. Not sure if finding the server path can help there. For example, the offline version's files might be located at "http://localhost/foo/" while the online version's files are at "http://foo.com/" (and not "http://foo.com/foo/" ).

Because of this I can't use absolute web paths, and therefore I can't include the same thing in any directory. One workaround might be to use something like this:

In the external script "functions.php"
CODE
<?php
function foo($dir_level)
{
    echo '<img src="'.$dir_level.'logo.jpg" alt="">';
}
?>


In files located in the web root directory:
CODE
<?php
include 'functions.php';
foo(''); // should return <img src="dog.jpg" alt="">
?>


In files located in child directories of the web root:
CODE
<?php
include '../functions.php';
foo('../'); // should return <img src="../dog.jpg" alt="">
?>



My hat off to you sir, and with very many thanks!!!!! The beauty of your approach is that it is very simple and logical!
I replaced the echo function in functions.php file in your illustration with an include function for the included file, and it works in the root directry, as well as the child directories!

On a personal note:
I am hoping to open a web site about my spare time project. (And, I truly mean spare time, when spare time is virtually non-existing. It have been working on it - on and of for 3+ years, and I still do not know when it will be ready for preview, leave alone when it will be ready for public view). The subject of including - both in SSI and PHP, is becoming a small, but an essential piece of the jig-saw puzzle that will be the main subject of this site.

Now, I will come to the point: I intend include a small write-up about using relative paths in SSI vs PHP when including files using each method. I would like to use your approach for PHP and I would like to give the full credit you deserve on that page/segment.

I do not know how it works, but I am under the impression that you can send me an internal e-mail through this board, without either one of us needing to post our e-mail addresses in public. If my assumption is correct, please let me know if you will give me permission to do so and how you would like me to formulate this credit, including a link to your site - if applicable.

Again thanks!
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Mar 25 2007, 10:08 PM
Post #19


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
Joined: 9-August 06
Member No.: 6



Now you've lost me. You still have to use different paths depending on where the file is so what's the gain? Why not use a path relative to the including file directly in the inlude()? blink.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Mar 26 2007, 08:46 AM
Post #20


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



QUOTE(Dr Z @ Mar 26 2007, 04:58 AM) *

My hat off to you sir, and with very many thanks!!!!! The beauty of your approach is that it is very simple and logical!

blush.gif No no, there must be better ways to do this.

QUOTE
I replaced the echo function in functions.php file in your illustration with an include function for the included file, and it works in the root directry, as well as the child directories!

But like pandy wrote, why not put the included content inside functions.php then, since you're already including that file? My little scheme is mostly for link, image or javascript file paths, etc. Just in case: note that the path to functions.php depends on the directory, i.e. in a child directory you must use "include '../functions.php'".

QUOTE
I would like to use your approach for PHP and I would like to give the full credit you deserve on that page/segment.

You're welcome, but credits are not necessary! smile.gif I don't even have a web site you could link to anyway. wacko.gif

QUOTE
I am under the impression that you can send me an internal e-mail through this board, without either one of us needing to post our e-mail addresses in public.

That's correct, you just click the PM (=Personal Message) button to the left under a member's avatar, or you can access the PM function from your Control Panel.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

3 Pages V  1 2 3 >
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: 26th April 2024 - 12:18 AM