![]() |
|
PHP plugin that controls your too many database connections - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: PHP (https://sinister.ly/Forum-PHP) +--- Thread: PHP plugin that controls your too many database connections (/Thread-PHP-plugin-that-controls-your-too-many-database-connections) |
PHP plugin that controls your too many database connections - redN00ws - 05-22-2013 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 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 RE: PHP plugin that controls your too many database connections - zomgwtfbbq - 05-27-2013 Never use ereg functions, they are deprecated. Instead of ereg_replace you should preg_replace. RE: PHP plugin that controls your too many database connections - webbiz - 08-01-2013 Line 10 in the script is the only one with the ereg_replace function that can be replaced with preg_replace with the same arguments and order. RE: PHP plugin that controls your too many database connections - redN00ws - 08-01-2013 (05-27-2013, 11:18 AM)zomgwtfbbq Wrote: Never use ereg functions, they are deprecated. Instead of ereg_replace you should preg_replace. (08-01-2013, 10:15 PM)webbiz Wrote: Line 10 in the script is the only one with the ereg_replace function that can be replaced with preg_replace with the same arguments and order. I've changed the ereg_replace in preg_replace, Thanks... RE: PHP plugin that controls your too many database connections - sitehost - 08-06-2013 Thanks for the Script, I was needing This.. A+++ RE: PHP plugin that controls your too many database connections - redN00ws - 08-06-2013 (08-06-2013, 01:58 PM)sitehost Wrote: Thanks for the Script, I was needing This.. A+++You're welcome, sitehost. I'm glad it's useful for you :Smile: |