Forum Moderators: coopster
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?
isn't the actual path what is needed?
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...
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.phpAnd 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
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...]
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...]
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.