Re: 'undefined function' error if I use a fully-qualifed include



Puzzled wrote:
> Oli Filth wrote:
>
> >Puzzled said the following on 18/10/2005 17:54:
> >> If I use a fully qualified include call
> >>
> >> include ( 'http://localhost/subtree/filename.php')
> >>
> >> I get an 'undefined function' error when calling a routine that's
> >> defined in that file.
> >
> >This is an absolute *HTTP* URL, not an absolute *filesystem* URL. So it
> >obtains filename.php by performing an HTTP request to the specified
> >server, which just happens to be "localhost" in this case.
> >
> >And of course, a server set up to parse and execute PHP files will parse
> >and execute filename.php when it's requested, so all your include() call
> >sees is the output result of filename.php.
>
> I think I must be missing something important still, because I don't
> understand what you said. Check me on this, would you?
>
> A URL is just another filesystem reference, but using a highly
> generalized syntax that's independent of o/s and physical
> location--like NFS, but more general. So any processing that would
> be done or not done to a file would be independent of the type of
> reference used to retrieve it.

In this case, not really, because the URL is prefixed with "http://";,
which specifies "Go and get this resource using the HTTP protocol".

The fact is that you're requesting "filename.php" via HTTP. PHP doesn't
know that you're expecting it to implicitly convert
"http://localhost/..."; to a local filesystem reference, so it simply
sends out an HTTP request for that file, just as if you'd typed
"http://localhost/.../filename.php"; into your browser.

The request just so happens to be to your own server, which serves this
request by processing filename.php spitting the output back as the HTTP
response. This HTTP response is then included into the original PHP
file.

> When the php interpreter knows it's operating on an include file, it
> should know enough not to start a new symbol table, but instead add
> things like function definitions to the existing symbol table it
> started building when it read up the outermost file.

This is impossible, because filename.php is processed in a completely
separate instance to the original PHP file, serving an entirely
separate HTTP request.

> Is that what's causing the problem, the fact that I'm using a .php
> extension on the include file?
>
> That would make a crazy sort of sense, since the php interpreter would
> do its own filesystem fetches, but might hand any url reference, even
> one for an include, off to Apache to satisfy. And somewhere in that
> process, the fact that it's just an include file is getting pushed and
> the file is being interpreted as though it were a completely new job.
> Whereas if it had some .inc or .foo extension, Apache wouldn't try to
> pass it through the interpreter, but would just hand it over.

Yup, that's the explanation in a nutshell (except that the first PHP
instance doesn't request filename.php directly from Apache, it does it
via an HTTP request which lands at Apache indirectly).

The simplest solution would be to not include() local files via HTTP.

Even if you renamed your include files to .inc extensions, this would
be insecure, since anyone could request them via HTTP and view your
source code.

--
Oli

.



Relevant Pages

  • Re: a gentle nudge
    ... I volunteered for a church project whereby I need my PHP page to send ... off a HTTP request to third party web site that provides a service. ... Is that part of the http package, too, or do I need something ...
    (comp.lang.php)
  • Re: DES Encryption Java for the Basic authentication PHP
    ... HTTP Basic Authentication uses no encryption at all. ... The web server checks the HTTP request packet for a special ... Now you can use this with PHP. ...
    (comp.lang.java.programmer)
  • Re: how does the apache server and php interpreter handles request?
    ... >I'm wondering how the apache server and php interepreter handles ... >if apache server passes the request to php interepreter and php ...
    (comp.lang.php)
  • Re: newbie questions...
    ... > are there any good PHP compilers... ... with PHP must go over HTTP (using a HTTP GET or HTTP POST request). ... Connectionless interactions don't allow sessions to "persist". ...
    (comp.lang.php)
  • Re: [PHP] Re: File Upload - post_max_size and upload_max_filesize in GBs
    ... So it seems applet posts complete data to that php file handling script,*as soon as upload button is clicked*, rather than uploading first and then posting to php. ... As that seems to be the case; there is no real benefit in using applet (in case of http) other than that progress bar which is already supported by php 5.2 ... I think you need to read up on how an HTTP POST request works when it's handled by PHP because some of what you've written there indicates that you don't quite have it. ...
    (php.general)