1

I have lots of websites, where i use a lot of includes on. Those files I include are on an external include-server. My problem is: I want to make those files redundant, so if the include server goes down, they are taken from my second include server. Doing that manually on each website will take by far too long, so I wonder if there is a way to do it for instance on the server-side (so if the server is down it forwards to the other server).

Here is an example of how I usually include my files:

<?php

    $url = 'http://myincludeserver.com/folder/fileiwanttoinclude.php';

    function get_data($url) 
    {
      $ch = curl_init($url);
      $timeout = 5;
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $_REQUEST);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
      $data = curl_exec($ch);
      curl_close($ch);

      return $data;
    }

    $returned_content = get_data($url);

    if(!empty($returned_content))
    {
      echo $returned_content;
    }
    else 
    {
      include('includes/local_error_message.php');  
    };

  ?>

Thanks for reading!

6
  • 1
    Just check if the include succeeds and if not include from the other one. Commented Aug 9, 2016 at 9:42
  • use Jquery-ajax and get response code by using $.ajax({ //... success: function(data, textStatus, xhr) { console.log(xhr.status); }, complete: function(xhr, textStatus) { console.log(xhr.status); } }); this then you know server is failed or not Commented Aug 9, 2016 at 9:51
  • thanks for the answer, but thats the easy part! If i do it like this, I'd have to do it on every include i use (which are like 1000+). Im looking for a way to do it so i dont have to edit every single file on every single website Commented Aug 9, 2016 at 10:12
  • @jogoe are you saying the get_data function has been duplicated 1000+ times? Commented Nov 7, 2016 at 16:27
  • 1
    You do not really transfer code you want to include/execute over an unsecured http connection, do you? Honestly, that is a horrible way to use code from a different spot, the overhead of loading the files over the net is extremely high, and dangerous too. Use an NFS to host common files and for gods sake, don't use the internet for this! Commented Nov 7, 2016 at 19:03

1 Answer 1

1
+50

Short answer:

You're more than likely going to want to refactor your code.

Longer answer:

If you truly want to do this at the server level then you're looking at implementing a "failover." You can read the wikipedia article, or this howto guide for a more in-depth explanation. To explain it simply, you would basically need 3 web servers:

  1. Your include server
  2. A backup server
  3. A monitoring / primary server

It sounds like you've already got all three, but bullet three would ideally be a service provided through a third-party for extra redundancy to handle the DNS (there could still be downtime as DNS updates are being propagated). Of course, this introduces several gotchas that might have you end up refactoring anyway. For example you might run into load balancing challenges; your application now needs to consider shared resources between servers such as anything written to disk, sessions or databases. Tools like HAProxy can help.

The simpler option, especially if the domains associated with the includes are hidden from the user, is to refactor and simply replace bullet three with a script similar to your get_data function:

function ping($domain) {
    $ch = curl_init($domain);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    curl_close($ch);
    return $response ? true : false;
}

$server1 = 'http://example.com';
$server2 = 'http://google.com';

if (ping($server1)) {
    return $server1;
} else {
    return $server2;
}
exit;

This would require you to update all of your files, but the good news is that you can automate the process by traversing all of your PHP files and replace the code via regex or by using a tokenizer. How you implement this option is entirely dependent on your actual code along with any differences between each site.

The only caveat here is that it could potentially double the hits to your server, so it would probably be better to use it in such a way that you're setting an environment or global variable and then have it execute periodically through cron.

I hope that helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for looking into my problem, and coming up with those nice ideas! I'm going to look into it and try it out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.