tags:

views:

1377

answers:

5

hi, I need to echo an include from my php function. I have tried the below but neither work? Does php support this within a function?

echo "<?php include ('http://www.mysite.com/script.php'); ?>";
echo "include (\"http://www.mysite.com/script.php\");";

Thanks

+1  A: 

Echo prints something to the output buffer - it's not parsed by PHP. If you want to include something, just do it

include ('http://www.mysite.com/script.php');

You don't need to print out PHP source code, when you're writing PHP source code.

Adam Wright
hi, I tried this and get a error saying direct file access is not allowed
Elliott
Sounds like allow_url_fopen is turned off. If your host permits it, you can try putting "php_value allow_url_fopen 1" in a .htaccess file.
ceejayoz
His question was how to do a particluar task, not whether or not eh should. Telling him what he does and does not need to do does not help answer the question. Unless asked, his strategy is up to him.
KOGI
+2  A: 

Just do:

include("http://www.mysite.com/script.php");

Or:

echo file_get_contents("http://www.mysite.com/script.php");

Notes:

  • This may slow down your page due to network latency or if the other server is slow.
  • This requires allow_url_fopen to be on for your PHP installation. Some hosts turn it off.
  • This will not give you the PHP code, it'll give you the HTML/text output.
ceejayoz
thanks file_get_contents works :)
Elliott
A: 

Not really sure what you're asking, but you can't really include something via http and expect to see code, since the server will parse the file.

If "script.php" is a local file, you could try something like:

$file = file_get_contents('script.php');
echo $file;
Pavel Lishin
A: 

Shortest way is:

readfile('http://www.mysite.com/script.php');

That will directly output the file.

Matt