SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I use a function that's in a file on another server?

    I am trying to use php's "include" to include a script that's on a separate server.

    For example purposes the file's name is "library.fn.php" located at www.example.com/functions/
    library.fn.php has the function userName().

    I need to call the function that's in "library.fn.php" from www.newexample.com.
    In my index.php for newexample.com I have:
    Code PHP:
    <?php 
    include 'www.example.com/functions/library.fn.php';
    userName();
    ?>

    When I run www.newexample.com I receive the error stating userName() is undefined.

    To make sure the code indeed included library.fn.php I added an echo statement to library.fn.php and the echo statement printed fine when you refresh www.newexample.com. However, the functions are still undefined.

    How can I use a function that's in a file on another server?

    I tried:
    fopen("http://www.example.com/functions/library.fn.php/", "r");
    readfile('http://www.example.com/functions/library.fn.php/');
    file_get_contents('http://www.example.com/functions/library.fn.php/');

    BTW my "URL fopen wrappers" are set to "On" in my php.ini file

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The documentation for include() address this.

    If you rename the remote file to .txt instead of .php, it will work. But... don't do this. Using include on a url is a serious wtf. You're relying on a slow, low reliability network connection for your code to work. It's also dangerous.

  3. #3
    SitePoint Enthusiast premiumscripts's Avatar
    Join Date
    Aug 2009
    Location
    PremiumScripts.com
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's very insecure. What you are looking for is to make this into a web service.

    Simply change the function on the other website to return the result from the function, not the function code. Then you can just use file_get_contents or whatever to get the result.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    If the script from the other server belongs to you then why are you doing it this way?

    Couldn't you simply WGET or cURL the file over to this server nightly?

    Permitting your server to even include stuff from other places opens a big security hole.

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    If you rename the remote file to .txt instead of .php, it will work.
    Thanks, that worked perfectly.


    I do own the site with the function library but the current site and future sites that I'm building aren't for me. The library functions are all part of a Content Management System that I built. I'm simply charging for the use of the service.

    I figured if I externally included the function.library on the sites that I build, if a client decides they don't want the CMS anymore they can simply stop paying and on my site that host the function.lib I could remove the functions until they wished to continue service.

    It's a way for me to control who's paying for what.

    Would any of you recommend a better way for me to achieve my goal with out having to externally "include" a file from my server?

    If I gave all the functions for the CMS to my clients, how would I stop them from using the service if they aren't paying, meanwhile the library rest on their server already?

  6. #6
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about using RPC? Would give you the opportunity to authorize the calling system, and of course prevent downloading of the files..

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not familiar with RPC.

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    So, if your site crashes then ALL of your clients loose their sites.

    Maybe you need to rethink that?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're definitely right about that one, which is why I'm looking for an alternative.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •