By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,930 Members | 633 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,930 IT Pros & Developers. It's quick & easy.

Is it possible to return a value other than true or false via an include over HTTP?

P: n/a
REFERENCE:
http://us4.php.net/manual/en/function.include.php
Example 16-7. include() through HTTP
Example 16-10. include() and the return() statement

--------------------
return.php
--------------------
<?php

$var = 'PHP';

return $var;

?>

--------------------
noreturn.php
--------------------
<?php

$var = 'PHP';

?>

--------------------
testreturns.php
--------------------
<?php

$foo = include 'http://www.domainwhatever.com/return.php';

echo $foo; // should print 'PHP', but only prints 1

$bar = include 'http://www.domainwhatever.com/noreturn.php';

echo $bar; // prints 1

?>
--------------------

So, is it possible to return a value other than true or false via an
include over HTTP? If not, is there another method of returning a value
from a remote PHP file or function? Thanks.

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Aeon Twilight wrote:
So, is it possible to return a value other than true or false via an
include over HTTP? If not, is there another method of returning a
value from a remote PHP file or function? Thanks.


Two things to remember here:

1. The allow_url_fopen directive should be enabled in the php.ini file
2. The remote code is parsed on the remote server before it's returned.

So, when the remote file prints "hello", you will see this as the output.
However, a return statement results in nothing when parsed on the remote
server. Only when used locally, you will able to store something in a
variable.

BTW, the integer is returned by the include construct to indicate success or
failure. With output buffering you can capture the output of the included
remote file in a variable without this indication:

ob_start();
include "http://...";
$content = ob_get_contents();
ob_end_clean();

print $content;
JW
Jul 17 '05 #2

P: n/a
Have you considered using SOAP? It seems like a better method of
RPC'ing over the web rather than your current methodology.

check out the Pear SOAP package: http://pear.php.net/package/SOAP

Jul 17 '05 #3

P: n/a
NC
Aeon Twilight wrote:

--------------------
return.php
--------------------
<?php
$var = 'PHP';
return $var;
?>

--------------------
noreturn.php
--------------------
<?php
$var = 'PHP';
?>

--------------------
testreturns.php
--------------------
<?php
$foo = include 'http://www.domainwhatever.com/return.php';
echo $foo; // should print 'PHP', but only prints 1
$bar = include 'http://www.domainwhatever.com/noreturn.php';
echo $bar; // prints 1
?>
--------------------

So, is it possible to return a value other than true or false
via an include over HTTP?


Why do you need to return anything? All variables in the
included code become available in the including code after
the include(). So you can simply write:

include 'http://www.domainwhatever.com/return.php';
echo $var; // should print 'PHP'

Cheers,
NC

Jul 17 '05 #4

P: n/a
NC (nc@iname.com) wrote:
: Aeon Twilight wrote:
: >
: > --------------------
: > return.php
: > --------------------
: > <?php
: > $var = 'PHP';
: > return $var;
: > ?>
: >
: > --------------------
: > noreturn.php
: > --------------------
: > <?php
: > $var = 'PHP';
: > ?>
: >
: > --------------------
: > testreturns.php
: > --------------------
: > <?php
: > $foo = include 'http://www.domainwhatever.com/return.php';
: > echo $foo; // should print 'PHP', but only prints 1
: > $bar = include 'http://www.domainwhatever.com/noreturn.php';
: > echo $bar; // prints 1
: > ?>
: > --------------------
: >
: > So, is it possible to return a value other than true or false
: > via an include over HTTP?

: Why do you need to return anything?

Perhaps just because the manual says you can?

However, http://us4.php.net/include/ says "[1] This is not, however,
possible when including remote files [2] unless the output of the remote
file has valid PHP start and end tags (as with any local file). You can
declare the needed variables within those tags and they will be introduced
at whichever point the file was included. "

To me that's not clear whether section [2] means you can do it as long as
you output the tags, or whether section [2] says you can't but there is a
workaround, which it then descibes. (and which is also mentioned below).

You could try "echo'ing" the <?php and ?> tags to see if that changes
anything. something like
<?php echo '<?php'; $var='XXX'; echo '?>' ?> though if it works at
all then you may have to play with some combinations of syntax to find the
one that works (assuming it works at all).

All variables in the
: included code become available in the including code after
: the include(). So you can simply write:

: include 'http://www.domainwhatever.com/return.php';
: echo $var; // should print 'PHP'

: Cheers,
: NC
--

This space not for rent.
Jul 17 '05 #5

P: n/a
"Aeon Twilight" <rb**********@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
REFERENCE:
http://us4.php.net/manual/en/function.include.php
Example 16-7. include() through HTTP
Example 16-10. include() and the return() statement

--------------------
return.php
--------------------
<?php

$var = 'PHP';

return $var;

?>

--------------------
noreturn.php
--------------------
<?php

$var = 'PHP';

?>

--------------------
testreturns.php
--------------------
<?php

$foo = include 'http://www.domainwhatever.com/return.php';

echo $foo; // should print 'PHP', but only prints 1

$bar = include 'http://www.domainwhatever.com/noreturn.php';

echo $bar; // prints 1

?>
--------------------

So, is it possible to return a value other than true or false via an
include over HTTP? If not, is there another method of returning a value
from a remote PHP file or function? Thanks.


Exactly what are you trying to accomplish here? Your approach is probably
flawed, as there is no valid reason to include an URL for most intents and
purposes.
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.