views:

57

answers:

3

is it possible to use php to redirect users to page i.e. busy.php when the server is busy or overcrowded, or something similiar? thanks :))

+1  A: 

You could use PHP Internal Functions like memory-get-usage http://php.net/manual/de/function.memory-get-usage.php or access a Shell Script that gives you some kind of Information about the current load of the Server. And then depending on that Information set a Redirect via Headers.

However, Remember that if your server breaks down, most likely the PHP Script wouldn't be executed and no redirect would happen. So, depending on your Infrastructure you can handle this over a secondary Server (a Load-Balancer perhaps).

If you can narrow down the most likely cause of a breakdown, try to fetch it there, for example if your MySQL Connection fails, fetch that, and direct the User to your "busy page".

Hannes
Doesn't that function just tell you the memory usage of the current PHP process, not a system wide total?
Alex JL
+1  A: 

The best aproach is to use a load balancer but just in case you can redirect to a busy page (that can't use DB connection at all) with this piece of code on your setup class:

class [...] {
    [...]
    public function connect(){
        $this->conn = @mysql_connect ([...]) 
            or $this->dbError("Failed MySQL connection");
        [...]
    }

    private function dbError($msg){
        include("busy.php");
        die();
    }
}
Coquevas
hmmm, would prefer try/fetch and a redirect to an existing static page (temporary redirect to prevent an SEO mess)
Hannes
instead of 30X redirect, my busy page uses 'HTTP 503 Service Temporarily Unavailable' and 'Retry-After: 3600' that is, in my opinion, better because prevent the busy page to be indexed
Coquevas
+1  A: 

Never used it but sys_getloadavg looks like it does what you are looking for:

<?php
$load = sys_getloadavg();
if ($load[0] > 80) {
    header('HTTP/1.1 503 Too busy, try again later');
    die('Server too busy. Please try again later.');
}
?>
Gordon