Jump to content

URL Redirect When Site is Slow


transfield

Recommended Posts

Hello,

I've got 2 websites hosted on 2 different servers. www.a.com is my main site & www.b.com is my so called mirror site.

 

The server on which www.a.com can be quite slow at times.

 

I'm looking for a code(preferably php) that will re-direct my visitors from www.a.com to www.b.com if the pages on the former takes more than 5 seconds to open.

 

I uderstand that fsockopen is supposed to do the job but I don't know how to get it working.

 

How do I achieve this?

 

Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/129999-url-redirect-when-site-is-slow/
Share on other sites

The following function will give you your server load as a PHP variable. Test it when your server's running ok. Test it when your server's running slow. Set a limit and the do an IF statement with a meta refresh to your 'mirror server'...

 

function getServerLoad($windows = false)
{
    $os = strtolower(PHP_OS);
    if (strpos($os, "win") === false) {
        if (file_exists("/proc/loadavg")) {
            $data = file_get_contents("/proc/loadavg");
            $load = explode(' ', $data);
            return $load[0];

        } elseif (function_exists("shell_exec")) {

            $load = explode(' ', `uptime`);
            return $load[count($load)-1];

        } else {

            return false;
        }

    } elseif($windows) {

        if(class_exists("COM")) {
            $wmi = new COM("WinMgmts:\\\\.");
            $cpus = $wmi->InstancesOf("Win32_Processor");
     
             $cpuload = 0;
             $i = 0;

            if(version_compare('4.50.0', PHP_VERSION) == 1) {
                // PHP 4
                while ($cpu = $cpus->Next()) {
                    $cpuload += $cpu->LoadPercentage;
                    $i++;
                }

            } else {

                // PHP 5
                foreach ( $cpus as $cpu ) {
                    $cpuload += $cpu->LoadPercentage;
                    $i++;
                }
            }

             $cpuload = round($cpuload / $i, 2);
             return "$cpuload%";

        } else {

            return false;
        }
    }
}

Archived

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

×
×
  • Create New...

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.