remote include doesn't work with class

I have been including local .php files in a script succesfully for a
while. But now I want it to be a remote include i.e.

include ("http://myserver.co.uk/includes/classLib.php");

and I get

Cannot instantiate non-existent class:

I know the path is correct because if I include simply a line of text
to echo then it works fine. Are there any issues with variable scope
when including remote classes please ? My class definition is simply

class AdminLib {
blah blah;
blah;
blah;
}

do I have to declare it global or anything ? Thanks.
charliefortune [ Mi, 01 August 2007 18:04 ] [ ID #1783974 ]

Re: remote include doesn't work with class

On Wed, 01 Aug 2007 18:04:14 +0200, charliefortune
<google [at] charliefortune.com> wrote:

> I have been including local .php files in a script succesfully for a
> while. But now I want it to be a remote include i.e.
>
> include ("http://myserver.co.uk/includes/classLib.php");
>
> and I get
>
> Cannot instantiate non-existent class:
>
> I know the path is correct because if I include simply a line of text
> to echo then it works fine. Are there any issues with variable scope
> when including remote classes please ? My class definition is simply
>
> class AdminLib {
> blah blah;
> blah;
> blah;
> }
>
> do I have to declare it global or anything ? Thanks.

If the include works fine (no allow_url_fopen or the remote include
thingy): don't forget they need to start & end with php opening & closing
tags... Else it's 'just content'.

Class definitions have no scope, allthough PHP6 might have support for
namespaces.
--
Rik Wasmus
luiheidsgoeroe [ Mi, 01 August 2007 18:16 ] [ ID #1783975 ]

Re: remote include doesn't work with class

On 1 Aug, 17:16, Rik <luiheidsgoe... [at] hotmail.com> wrote:
> On Wed, 01 Aug 2007 18:04:14 +0200, charliefortune
>
>
>
> <goo... [at] charliefortune.com> wrote:
> > I have been including local .php files in a script succesfully for a
> > while. But now I want it to be a remote include i.e.
>
> > include ("http://myserver.co.uk/includes/classLib.php");
>
> > and I get
>
> > Cannot instantiate non-existent class:
>
> > I know the path is correct because if I include simply a line of text
> > to echo then it works fine. Are there any issues with variable scope
> > when including remote classes please ? My class definition is simply
>
> > class AdminLib {
> > blah blah;
> > blah;
> > blah;
> > }
>
> > do I have to declare it global or anything ? Thanks.
>
> If the include works fine (no allow_url_fopen or the remote include
> thingy): don't forget they need to start & end with php opening & closing
> tags... Else it's 'just content'.
>
> Class definitions have no scope, allthough PHP6 might have support for
> namespaces.
> --
> Rik Wasmus

The include is wrapped in <?php ?> tags so that's not the problem. I
don't know about the allow_url_fopen stuff. Is there an fread
alternative I could do to read the file and eval() it perhaps, just to
get it working ?
charliefortune [ Mi, 01 August 2007 18:19 ] [ ID #1783976 ]

Re: remote include doesn't work with class

On Wed, 01 Aug 2007 18:19:45 +0200, charliefortune
<google [at] charliefortune.com> wrote:

> On 1 Aug, 17:16, Rik <luiheidsgoe... [at] hotmail.com> wrote:
>> On Wed, 01 Aug 2007 18:04:14 +0200, charliefortune
>>
>>
>>
>> <goo... [at] charliefortune.com> wrote:
>> > I have been including local .php files in a script succesfully for a
>> > while. But now I want it to be a remote include i.e.
>>
>> > include ("http://myserver.co.uk/includes/classLib.php");
>>
>> > and I get
>>
>> > Cannot instantiate non-existent class:
>>
>> > I know the path is correct because if I include simply a line of text
>> > to echo then it works fine. Are there any issues with variable scope
>> > when including remote classes please ? My class definition is simply
>>
>> > class AdminLib {
>> > blah blah;
>> > blah;
>> > blah;
>> > }
>>
>> > do I have to declare it global or anything ? Thanks.
>>
>> If the include works fine (no allow_url_fopen or the remote include
>> thingy): don't forget they need to start & end with php opening &
>> closing
>> tags... Else it's 'just content'.
>>
>> Class definitions have no scope, allthough PHP6 might have support for
>> namespaces.
>
> The include is wrapped in <?php ?> tags so that's not the problem.

I sure hope it doesn't get run, but outputs php code. Do you see your
class definition if you look at the url in a browser? (well in the source
of the page, the <?php ?> output will effectively hide it in most...

> I
> don't know about the allow_url_fopen stuff.

Well, for your settings:
echo 'Remote opening ',ini_get('allow_url_fopen') ? 'allowed':'disallowed';
echo 'Remote including ',ini_get('allow_url_include') ?
'allowed':'disallowed';

> Is there an fread
> alternative I could do to read the file and eval() it perhaps, just to
> get it working ?

That would only be necesary if allow_url_include was set to false.

--
Rik Wasmus
luiheidsgoeroe [ Mi, 01 August 2007 18:27 ] [ ID #1783977 ]

Re: remote include doesn't work with class

On Aug 1, 12:19 pm, charliefortune <goo... [at] charliefortune.com> wrote:
> On 1 Aug, 17:16, Rik <luiheidsgoe... [at] hotmail.com> wrote:
>
>
>
> > On Wed, 01 Aug 2007 18:04:14 +0200, charliefortune
>
> > <goo... [at] charliefortune.com> wrote:
> > > I have been including local .php files in a script succesfully for a
> > > while. But now I want it to be a remote include i.e.
>
> > > include ("http://myserver.co.uk/includes/classLib.php");
>
> > > and I get
>
> > > Cannot instantiate non-existent class:
>
> > > I know the path is correct because if I include simply a line of text
> > > to echo then it works fine. Are there any issues with variable scope
> > > when including remote classes please ? My class definition is simply
>
> > > class AdminLib {
> > > blah blah;
> > > blah;
> > > blah;
> > > }
>
> > > do I have to declare it global or anything ? Thanks.
>
> > If the include works fine (no allow_url_fopen or the remote include
> > thingy): don't forget they need to start & end with php opening & closing
> > tags... Else it's 'just content'.
>
> > Class definitions have no scope, allthough PHP6 might have support for
> > namespaces.
> > --
> > Rik Wasmus
>
> The include is wrapped in <?php ?> tags so that's not the problem. I
> don't know about the allow_url_fopen stuff. Is there an fread
> alternative I could do to read the file and eval() it perhaps, just to
> get it working ?

When you do a remote include like that the you usually don't get the
actual code back since the remote server is typically configured to
run it through PHP before sending the output. Suppose your remote
file has this in it:

<?php
class Foo {

}
?>

If you request that file in your browser what do you get? You get
nothing since that script has no output. So, when you do the include,
he doesn't get any output either.

Remember: when you request a PHP file through a webserver you don't
actually get the PHP code back -- you get the output of the PHP script
back.
zeldorblat [ Mi, 01 August 2007 18:30 ] [ ID #1783978 ]

Re: remote include doesn't work with class

On 1 Aug, 17:30, ZeldorBlat <zeldorb... [at] gmail.com> wrote:
> On Aug 1, 12:19 pm, charliefortune <goo... [at] charliefortune.com> wrote:
>
>
>
> > On 1 Aug, 17:16, Rik <luiheidsgoe... [at] hotmail.com> wrote:
>
> > > On Wed, 01 Aug 2007 18:04:14 +0200, charliefortune
>
> > > <goo... [at] charliefortune.com> wrote:
> > > > I have been including local .php files in a script succesfully for a
> > > > while. But now I want it to be a remote include i.e.
>
> > > > include ("http://myserver.co.uk/includes/classLib.php");
>
> > > > and I get
>
> > > > Cannot instantiate non-existent class:
>
> > > > I know the path is correct because if I include simply a line of text
> > > > to echo then it works fine. Are there any issues with variable scope
> > > > when including remote classes please ? My class definition is simply
>
> > > > class AdminLib {
> > > > blah blah;
> > > > blah;
> > > > blah;
> > > > }
>
> > > > do I have to declare it global or anything ? Thanks.
>
> > > If the include works fine (no allow_url_fopen or the remote include
> > > thingy): don't forget they need to start & end with php opening & closing
> > > tags... Else it's 'just content'.
>
> > > Class definitions have no scope, allthough PHP6 might have support for
> > > namespaces.
> > > --
> > > Rik Wasmus
>
> > The include is wrapped in <?php ?> tags so that's not the problem. I
> > don't know about the allow_url_fopen stuff. Is there an fread
> > alternative I could do to read the file and eval() it perhaps, just to
> > get it working ?
>
> When you do a remote include like that the you usually don't get the
> actual code back since the remote server is typically configured to
> run it through PHP before sending the output. Suppose your remote
> file has this in it:
>
> <?php
> class Foo {
>
> }
>
> ?>
>
> If you request that file in your browser what do you get? You get
> nothing since that script has no output. So, when you do the include,
> he doesn't get any output either.
>
> Remember: when you request a PHP file through a webserver you don't
> actually get the PHP code back -- you get the output of the PHP script
> back.

When I go to the url, I get nothing because, like you say, the script
doesn't produce anything, it is just a single class definition, and is
being parsed before being returned.

So how does one go about reusing class definitions from remote
sources please ?
charliefortune [ Mi, 01 August 2007 19:26 ] [ ID #1783979 ]

Re: remote include doesn't work with class

On Wed, 01 Aug 2007 19:26:50 +0200, charliefortune
<google [at] charliefortune.com> wrote:

> On 1 Aug, 17:30, ZeldorBlat <zeldorb... [at] gmail.com> wrote:
>> On Aug 1, 12:19 pm, charliefortune <goo... [at] charliefortune.com> wrote:
>>
>>
>>
>> > On 1 Aug, 17:16, Rik <luiheidsgoe... [at] hotmail.com> wrote:
>>
>> > > On Wed, 01 Aug 2007 18:04:14 +0200, charliefortune
>>
>> > > <goo... [at] charliefortune.com> wrote:
>> > > > I have been including local .php files in a script succesfully
>> for a
>> > > > while. But now I want it to be a remote include i.e.
>>
>> > > > include ("http://myserver.co.uk/includes/classLib.php");
>>
>> > > > and I get
>>
>> > > > Cannot instantiate non-existent class:
>>
>> > > > I know the path is correct because if I include simply a line of
>> text
>> > > > to echo then it works fine. Are there any issues with variable
>> scope
>> > > > when including remote classes please ? My class definition is
>> simply
>>
>> > > > class AdminLib {
>> > > > blah blah;
>> > > > blah;
>> > > > blah;
>> > > > }
>>
>> > > > do I have to declare it global or anything ? Thanks.
>>
>> > > If the include works fine (no allow_url_fopen or the remote include
>> > > thingy): don't forget they need to start & end with php opening &
>> closing
>> > > tags... Else it's 'just content'.
>>
>> > > Class definitions have no scope, allthough PHP6 might have support
>> for
>> > > namespaces.
>>
>> > The include is wrapped in <?php ?> tags so that's not the problem. I
>> > don't know about the allow_url_fopen stuff. Is there an fread
>> > alternative I could do to read the file and eval() it perhaps, just to
>> > get it working ?
>>
>> When you do a remote include like that the you usually don't get the
>> actual code back since the remote server is typically configured to
>> run it through PHP before sending the output. Suppose your remote
>> file has this in it:
>>
>> <?php
>> class Foo {
>>
>> }
>>
>> ?>
>>
>> If you request that file in your browser what do you get? You get
>> nothing since that script has no output. So, when you do the include,
>> he doesn't get any output either.
>>
>> Remember: when you request a PHP file through a webserver you don't
>> actually get the PHP code back -- you get the output of the PHP script
>> back.
>
> When I go to the url, I get nothing because, like you say, the script
> doesn't produce anything, it is just a single class definition, and is
> being parsed before being returned.


On the server with the definition:
file: givememyclassdefinitioninplainphp.php
<?php
readfile('myClass.php');
?>

Then again, I would never never never get class definitions this way. Too
much overhead, huge security risks, slow, waiting for a
connection-error/incomplete file to happen etc.

>
> So how does one go about reusing class definitions from remote
> sources please ?
>

I would keep a local copy, keep a CVS or SVN repository of the class
definitions, and update now and then according to need.


--
Rik Wasmus
luiheidsgoeroe [ Mi, 01 August 2007 19:33 ] [ ID #1783980 ]

Re: remote include doesn't work with class

..oO(Rik)

>On Wed, 01 Aug 2007 19:26:50 +0200, charliefortune
><google [at] charliefortune.com> wrote:
>
>> When I go to the url, I get nothing because, like you say, the script
>> doesn't produce anything, it is just a single class definition, and is
>> being parsed before being returned.
>
>On the server with the definition:
>file: givememyclassdefinitioninplainphp.php
><?php
>readfile('myClass.php');
>?>

Or simply use a file extension other than '.php', so the server won't
parse the file. '.inc' or '.txt' would be fine.

>Then again, I would never never never get class definitions this way. Too
>much overhead, huge security risks, slow, waiting for a
>connection-error/incomplete file to happen etc.

ACK

Micha
Michael Fesser [ Mi, 01 August 2007 19:42 ] [ ID #1783981 ]

Re: remote include doesn't work with class

On Aug 1, 9:04 am, charliefortune <goo... [at] charliefortune.com> wrote:
>
> I have been including local .php files in a script succesfully for a
> while. But now I want it to be a remote include

And I want to build a perpetual motion engine. Neither is likely to
happen. If you include() a remote PHP file and the remote server has
PHP enabled, you are including the output, not the code.

> do I have to declare it global or anything ?

No, you have to have the file being included on your local machine.

Cheers,
NC
nc [ Mi, 01 August 2007 19:50 ] [ ID #1783983 ]

Re: remote include doesn't work with class

On Wed, 01 Aug 2007 19:42:24 +0200, Michael Fesser <netizen [at] gmx.de> wrote:

> .oO(Rik)
>
>> On Wed, 01 Aug 2007 19:26:50 +0200, charliefortune
>> <google [at] charliefortune.com> wrote:
>>
>>> When I go to the url, I get nothing because, like you say, the script
>>> doesn't produce anything, it is just a single class definition, and is
>>> being parsed before being returned.
>>
>> On the server with the definition:
>> file: givememyclassdefinitioninplainphp.php
>> <?php
>> readfile('myClass.php');
>> ?>
>
> Or simply use a file extension other than '.php', so the server won't
> parse the file. '.inc' or '.txt' would be fine.

Which would defeat the whole purpose of having your class in just one
single file. If he can rename it, why not just ftp it to its desired
location on alterations?
--
Rik Wasmus
luiheidsgoeroe [ Mi, 01 August 2007 20:03 ] [ ID #1783985 ]

Re: remote include doesn't work with class

..oO(Rik)

>On Wed, 01 Aug 2007 19:42:24 +0200, Michael Fesser <netizen [at] gmx.de> wrote:
>>
>> Or simply use a file extension other than '.php', so the server won't
>> parse the file. '.inc' or '.txt' would be fine.
>
>Which would defeat the whole purpose of having your class in just one
>single file. If he can rename it, why not just ftp it to its desired
>location on alterations?

Yep. Of course that's better.

Micha
Michael Fesser [ Mi, 01 August 2007 20:11 ] [ ID #1783986 ]

Re: remote include doesn't work with class

On 1 Aug, 18:50, NC <n... [at] iname.com> wrote:
> On Aug 1, 9:04 am, charliefortune <goo... [at] charliefortune.com> wrote:
>
>
>
> > I have been including local .php files in a script succesfully for a
> > while. But now I want it to be a remote include
>
> And I want to build a perpetual motion engine. Neither is likely to
> happen. If you include() a remote PHP file and the remote server has
> PHP enabled, you are including the output, not the code.
>
> > do I have to declare it global or anything ?
>
> No, you have to have the file being included on your local machine.
>
> Cheers,
>NC

Well I did it as a .inc, which works, but now my code gets output to
the browser if you visit the url. Is CVS the solution ?
charliefortune [ Mi, 01 August 2007 20:13 ] [ ID #1783987 ]

Re: remote include doesn't work with class

On Aug 1, 2:13 pm, charliefortune <goo... [at] charliefortune.com> wrote:
>
> Well I did it as a .inc, which works, but now my code gets output to
> the browser if you visit the url. Is CVS the solution ?

Exactly! That's one of the many reasons that it's a bad idea to do it
this way. As others have suggested you should keep a local copy --
how you do that is up to you. You could just FTP it each time you
make a change, you could use some sort of source control like CVS or
SVN, you could run a script every 60 seconds from cron that
automatically FTPs the file over -- the possibilities are endless.
zeldorblat [ Mi, 01 August 2007 20:45 ] [ ID #1783988 ]

Re: remote include doesn't work with class

On 1 Aug, 19:45, ZeldorBlat <zeldorb... [at] gmail.com> wrote:
> On Aug 1, 2:13 pm, charliefortune <goo... [at] charliefortune.com> wrote:
>
>
>
> > Well I did it as a .inc, which works, but now my code gets output to
> > the browser if you visit the url. Is CVS the solution ?
>
> Exactly! That's one of the many reasons that it's a bad idea to do it
> this way. As others have suggested you should keep a local copy --
> how you do that is up to you. You could just FTP it each time you
> make a change, you could use some sort of source control like CVS or
> SVN, you could run a script every 60 seconds from cron that
> automatically FTPs the file over -- the possibilities are endless.

sweet, thanks for all your help.
charliefortune [ Mi, 01 August 2007 20:53 ] [ ID #1783989 ]
PHP » alt.php » remote include doesn't work with class

Vorheriges Thema: Creating an auto-login page
Nächstes Thema: PHP Installation Problems