Jump to content


This topic is now archived and is closed to further replies.


Remote functions in PHP

Recommended Posts


I have two servers: Server A and Server B with a website. Server A contains some general useful code in php functions, which will be used in the website on Server B.

Now, how can I include the functionality in a php file on Server A in the website on Server B?

For example:
[myfunctionfile.php on Server A]
function MyFunction(myArgs)
  return "this is a test" + myArgs;

[myWebPage.php on Server B]

  print MyFunction("for server B");

The above should print out on myWebPage.php:

this is a test for server B

I already tried to rename the myfunctionfile.php to myfunctionfile.php.inc, but that didn't work.

Thanks in advance!

Share this post

Link to post
Share on other sites
I'm not real experienced in this, but I know that the way you are doing this cannot work for one reason.
You are retrieving the evaluated php page, not the raw page.
eg: include("http://www.servera.com/myfunctionfile.php"); includes the html page that you get when visiting the page on your browser, not the php textfile that your code is looking for.

Now I have three ideas, and i don't know how well any of them work, since I have never tried them.

1) You could rename myfunctionfile.php myfunctionfile.txt so that Apache doesn't try to execute it before sending it.
Downfalls: I don't know if Webpage.php will be able to figure out that the php commands in a text file are php commands and not text, but i suspect it will work as long as you surround the commands with <?php ?>
  Also, this will have the unfortunate effect of making your php function file publicly available for veiwing, but if this doesn't bother you, go ahead.

2) You could change the include functuion to say: require("ftp://username:password@www.servera.com/myfunctionfile.php");
Downfalls: This will not work with versions of PHP prior to PHP 4.3.0
  I don't know if it accepts FTP even after 4.3.0

3) If number 2 doesn't work, you could include something like this: (I have not actually tried this yet, and their might be some bugs)
$handle = fopen("temp.php", 'w');
$conn_id = ftp_connect("ftp://www.servera.com/myfunctionfile.php");
$login_result = ftp_login($conn_id, $YOURUSERNAME, $YOURPASSWORD);
if ((!$conn_id) || (!$login_result)) {
      echo "FTP connection has failed!";
if (!ftp_fget($conn_id, $handle, myfunctionfile.php, FTP_ASCII, 0))
      echo "Could not get myfunctionfile.php";

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.