PHP plugin that controls your too many database connections 05-22-2013, 10:39 PM
#1
This is a part of several functions I use to check and controle the health of a large website.
I made some adjustments to give you a better view of what I mean and hope I didn't make any mistakes.
This coding checks the amount of database threads at the moment someone visits one of the web pages and starts blocking some (DB-)traffic when the DB is having a hard time
Feel free to use it and improvements are always welcome
I made some adjustments to give you a better view of what I mean and hope I didn't make any mistakes.
This coding checks the amount of database threads at the moment someone visits one of the web pages and starts blocking some (DB-)traffic when the DB is having a hard time
Code:
<?php
//** CHECK DB LIMITS
//--> Make a database-connection
[Make a connectiont with your DB here]
$test = explode(' ', mysql_stat());
mysql_close();
$threads = preg_replace("Threads: ", "", $test[1]);
//--> If to many DB threads, disable pages for visitors accept user coming from some sites (eg. your own, google, monitoring sites)
// +90 threads : Block all traffic untill the DB calmes down again (probably in a few seconds)
if($threads > 90) :
$block = true;
if($block == true) :
echo "We're receiving to much traffic at the moment, please <a href=\"" . $_SERVER["REQUEST_URI"] . "\">RETRY</a> (Code T_$threads)"; die;
endif;
// +70 threads : block traffic but allow traffic coming from your own site, google and monitoring websites
elseif($threads > 70) :
$block = true;
$allowed = array("[yoursitename]", "amazon", "google", "pingdom");
foreach ($allowed as $k => $v) :
// allow google and pingdom crawler
if(strpos(gethostbyaddr($_SERVER['REMOTE_ADDR']), $v)) : $block = false; break;
// allow traffic coming from your own site
elseif(strpos(getenv("HTTP_REFERER"), $v)) : $block = false; break; endif;
endforeach;
// block all other traffic untill the DB calmes down again
if($block == true) :
echo "We're receiving to much traffic at the moment, please <a href=\"" . $_SERVER["REQUEST_URI"] . "\">RETRY</a> (Code T_$threads)"; die;
endif;
// +50 threads : block some traffic but be sure search engine spiders don't crawl your blocked page
elseif($threads > 50) :
if(strpos($_SERVER["REQUEST_URI"], "news")) :
$block = true;
$allowed = array("[yoursitename]", "amazon", "baidu", "bit", "crawl", "google", "msn", "pingdom", "tinyurl", "twitter", "yahoo", "yandex");
foreach ($allowed as $k => $v) :
if(strpos(gethostbyaddr($_SERVER['REMOTE_ADDR']), $v)) : $block = false; break;
elseif(strpos(getenv("HTTP_REFERER"), $v)) : $block = false; break; endif;
endforeach;
if(getenv("HTTP_REFERER") != "") : $block = false; endif;
if($block == true) :
echo "We're receiving to much traffic at the moment, please <a href=\"" . $_SERVER["REQUEST_URI"] . "\">RETRY</a> (Code T_$threads)"; die;
endif;
endif;
endif;
?>Feel free to use it and improvements are always welcome
redN00ws
Cleveridge - Ethical Hacking Lab
I'm looking for freelance vulnerability testers. Interested? Contact me...
Cleveridge - Ethical Hacking Lab
I'm looking for freelance vulnerability testers. Interested? Contact me...
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)