RE: [PHP] include() syntax
| From: | Jason Murray | Date: | Wed, 27 Sep 2000 06:56:52 +0000 |
| Subject: | RE: [PHP] include() syntax | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-17711@lists.php.net to get a copy of this message | ||
> I'm trying to work through include function syntax. I am working in a
> directory called "news" and trying to include a file in a
> directory next to it called "inc". Both directories are under "/dca"
> of the server's root.
>
> When I try to include "../inc/links.php" it works just fine,
> but I'd prefer to specify a pathname from the server root than a
> relative pathname. That way, I could use the same call from any level
> of the directory structure. I guess the problem is that php treats that
> as a URL, not a local pathname.
What you need to do is define elsewhere your root directory. Every site
I build has a single file that holds stuff like this, and its include()'d
in every page on the site. So, every page knows where the root directory
of the web server is.
My globals file contains:
$DocRoot = "/usr/local/apache/virtual/shadow.net.au/htdocs";
And then I do the include as such:
include("$DocRoot/something-i-need.php");
I find this is easy to maintain and makes the code extremely portable.
Most of my globals files now have multiple server definition blocks in
them so that when a site is deployed, one line needs to be changed to
adapt the scripts to their new home.
> When I try to include the file from the web server root path,
> "/dca/inc/links.php", I get a parse error "Warning: failed opening".
Unless you have a directory named dca under your computer's / directory,
this will not work. If you always include the right root directory of the
web server (usually somewhere under /usr/local/apache), then it'll work.
It'll also make it harder to port to another server because you'll need
to change every instance of it.
Remember, include() likes a FULL PATH since it's a command that operates
on the local filesystem by default.
> When I try this:
> include("http://" . $SERVER_NAME .
> "/dca/inc/linklist.php");
> The file gets included and executes, but no variables that
> were previously set are used, so the links.php does not produce
> the desired results.
Yes, think about why for a second ... when PHP launches a HTTP request
to localhost to grab the file you request, it gets ... parsed ... through
... PHP. ... which obviously, strips the PHP code. :)
Jason