Forum Moderators: coopster

Message Too Old, No Replies

Problem with include file.php

including a file which includes a file

         

louponne

1:01 pm on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On my site I have page.php which has:
include ../php/functions.ph

within my functions.php I have
include utilities.php (which also resides in /php/)

Now, of course depending on from where I call functions.php, I'm getting an error. I read somewhere that if I do, within functions.php:
include dirname(__FILE__)."/utilities.php";
that the path will always work itself out.

Will this indeed always work?

Is it the best way (or, anyway, a good way) to handle this problem?

vincevincevince

1:46 pm on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the BEST way is to use a full server path.
something like-
include("host/server/www/root/html/php/script.php");
you can get the path from PHPINFO()

louponne

2:20 pm on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi vincevincevince,
thanxthanxthanx for your help again!
Your solution does indeed seem the most reliable way on the web server - but I can't use that if I want to test on my home system, which will have a different path, without having to change that all the time.
Do you see any problems with the solution I propose?

vincevincevince

2:25 pm on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



include($_SERVER["DOCUMENT_ROOT"]."/php/utilities.php");

louponne

3:30 pm on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



nope - tried that, it's not working on my home machine. (I have EasyPHP installed if that matters at all) The
$_SERVER["DOCUMENT_ROOT"]
is producing the actual path on my machine instead of the Apache alias.

vincevincevince

3:52 pm on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



isn't the actual path what is needed? i tested it on a windows installation, it gives "C:\\server\\www" - and if i then add the path to my script to that it finds it fine...

on apache it gives again a fully qualified path "/home/webusers/virtualhost233/site2"

louponne

4:08 pm on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



isn't the actual path what is needed?

Actually, no, since I'm using aliasing. In other words, your code gives me
c:/folder/folder/file.php
while I need
[127.0.0.1...]

vincevincevince

5:21 pm on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if you use just include("utilities.php") then it will include it via local relative path - which is via the c:/folder/folder/file.php route...

remember apache and php live on your machine, and so they handle c:/folder/folder/file.php just fine... are you _sure_ that doesn't work?

if you include with an http:// path then you won't include the file - you'll just insert the result of the file after it's been parsed...

louponne

6:35 pm on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, no, since I'm using aliasing. In other words, your code gives me
c:/folder/folder/file.php
while I need
[127.0.0.1...]
I'm thinking I wasn't clear enough there - I wasn't inlcuding the file via an http address, I was saying that the code you suggest, $_SERVER["DOCUMENT_ROOT"] , was serving up c://etc where for the file to be found on my machine and be "seen" as php, it your code would have to produce 127.0.0.1/alias/file.php

And yes, I'm _sure_ - if I try to open up a .php file via a direct c:// address, the php isn't run - it's only run if I open it through my local server, located at 127.0.0.1

vincevincevince

6:45 pm on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i think i see what you're doing - it seems you are using include() somewhat not how it was intended*, but not to worry :-)

use $_SERVER["SERVER_ADDR"]

[*you say the php isn't run - when including php into php it's as if the code is copy&pasted into your file... that's why it may seem like it isn't run...]

louponne

8:39 pm on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you say the php isn't run - when including php into php it's as if the code is copy&pasted into your file... that's why it may seem like it isn't run...]

No - I see that I have been unclear again. On my local system (under win2k - remember, this is not a webserver, it's my home machine!) If I try to run *any* .php file from c:/directory/ etc instead of from 127.0.0.1/ it opens directly into the browser, just like a .htm file

vincevincevince

10:51 pm on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I try to run *any* .php file from c:/directory/ etc instead of from 127.0.0.1/ it opens directly into the browser, just like a .htm file

This is correct behaviour.

Let me outline how it works:

opening C:/something/file.php in internet explorer:
request --> disk --> file --> internet explorer
the file has not changed at all, and IE tries to render the php source code

opening [*localhost...] in internet explorer:
request --> apache --> file --> php --> internet explorer
ie php has parsed the code, and the php output goes to IE

INCLUDING C:/something/file.php from php:
request --> apache --> file --> php [finds include] --> apache --> C:/something/file.php --> apache --> php --> IE
ie the include file is passed to php

PHP does not access files in the same way as just a local reference C:/something/something does. PHP passes requests through apache, and apache causes the file to be parsed and processed by php.

-

Testing includes:

filea.php: <?php include("fileb.php");echo a();?>
fileb.php: <?php function a(){return "Included";}?>

now, if we look at [localhost...] we will see:
Included
We will NOT see:
<?php function a(){return "Included";}?>

If we were to change that to:

filea.php: <?php include("http:*//localhost/fileb.php");echo a();?>
fileb.php: <?php function a(){return "Included";}?>

then we would see nothing at all, as fileb.php is included in the post-parsed state (i.e. no output to browser is there - the same as we'd see if we went to fileb.php directly - it's been parsed BEFORE php included it, so the function is not declared).

Finally, using C:/something:

filea.php: <?php include("C:/something/fileb.php");echo a();?>
fileb.php: <?php function a(){return "Included";}?>

now, if we look at [localhost...] we will see:
Included
Just the same as if we include with a relative path.

That's how it works - apache & php SEE local files in the local system (C:/files/something) .... if you give it something in the form of (http://localhost/something) then it will only have access to what any other web-browser goig directly to that page can have access to.

i hope this clarifies the differences you are seeing here. now, try $_SERVER["DOCUMENT_ROOT"], as I believe if you are using include() for what it's meant to, it's the best solution. if you actually want to just stick into your including script the end result of the included file, then use $_SERVER["SERVER_ADDR"].

please do let me know how you get on - maybe you need to sticky me some specifics if you still can't fix it.