• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

PHP Include()

ashben

Moderator
Guys I have this query ..

I have two servers (say A and B) and I wish to include a PHP script on server A in a script on server B. So the main script on server B should have something like ..

PHP:
Include("serverA.inc.php");

How do I do that? Any workarounds?

Any help will be great!
 
Thanks but it doesn't work.

I have a test variable in a.php (on Server A) as ..

PHP:
$test = "test123";

While the calling script, b.php (on server B) has ..

PHP:
include ("http://www.serverA.com/a.php");
echo $test;

And the output of the above script is blank.

Please help!
 
This approach does work if both the PHP files are on the same server. Unfortunately, the project architecture won't permit form-based submission.
 
It doesn't have to be HTTP, but you'll have to remotely access either one of the files if they're on different servers, so you can probably do:

include("a.php"); # local
include("ftp://username:password@ftp.server.tld/path/to/file/b.php"); # remote

You cant parse vars with include ! this why...
try to move the var with a html form (method=post).
No, you can, and that's what PHP adopted from Perl:

include("$variable"); # calls a file whose URL or path is given through $variable
 
Last edited:
No, I mean you can have a path to a file in an include through the use of variables.

i.e.

$this = "a.file";
$path="/path/to/";
include("$this$path"); # will include /path/to/a.file
 
Last edited:
Originally posted by megapuzik
I think that you also do it with php....never tried it, but I think that you can...
Uhh...that was PHP. :rolleyes:

Perl scripts usually use require.
 
Originally posted by Ares
change
PHP:
$test = "test123";
to
PHP:
<?php
$test = "test123";
?>

and chmod to 777.

Err... I think he know's that if he knows a bit about PHP :p

Anyways, I wouldn't CHMOD to 777 unless you want everyone to be able to write to that file. 755 should do, I think. ;)
 
No one's answered your question yet so I guess it's up to me to burst your bubble. What you're trying to do can't be done. Including an external file won't include the file's contents, rather just it's output (an output that would be nothing if the external file is just a list of variables). I guess you could try printing out the variables in the external file (for example, instead of "$var='whatever';" you'd have "print '$var=\'whatever\';';") for the local one, but I'm not even sure if that would work... probably wouldn't, but worth a shot.
 
Ok. here's the inference:

I can include a remote file. What I did was that I renamed the remote file to b.inc instead of b.php. This way it can be included on both the remote & local server. The new problem however is that b.inc is publicly downloadable (ie: not safe).

Any suggestions?
 
That's essentially doing what I suggested, only you're just including a text file instead of a PHP file that prints out its script instead of executing it. I'm surprised it works, actually.

Here's what you could do: Rename it back to a .php file and do like I said with printing it out, then to the top add a little function that checks the referrer. If it's from a.php, then print out b.php, otherwise show nothing.

For a.php on the local server, just include the URL to b.php and force PHP to load it as it would a remote file.
 
Dusty:

Doing an explicit response like ie: echo "\$test = \"test123\";"; in a.php doesn't work either. It simply dumps the output as plain text rather than evaluating it as PHP code in b.php.
 
The only way I see it working without it being world readable is through FTP.
 
Yes... that doesn't work and your way (the one you mentioned in the other post, renaming it .inc) doesn't work for me either. As I suspected to begin with, it just isn't possible. Calling a remote file simply gets its output, it doesn't read it like it does a local file. You could do some workarounds to get the contents of a remote file, like using FTP as TPFKaN posted or writing a relay script on the remote server to feed information to a.php (or b.php or whichever is the local file), but in either scenario you couldn't include it without first saving it locally.
 
Back
Top