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 :))
views:
57answers:
3You 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".
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();
}
}
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.');
}
?>