Advertisement
opsftw

TBan v1.0

Mar 16th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.87 KB | None | 0 0
  1. <?php
  2. /* * * * * * * * * * * * * * * * * *
  3.  * TITLE: TBan v1.0                *
  4.  * AUTHOR: Terebic.                *
  5.  * DESCRIPTION: Automatically ban  *
  6.  * clients making to many requests *
  7.  * in to short of a time.          *
  8.  * * * * * * * * * * * * * * * * * */
  9.  
  10. /* * * * * * * * * *
  11.  * SCRIPT SETTINGS *
  12.  * * * * * * * * * */
  13.  
  14. # The rate (seconds) at witch to refresh the session unless user is temporarily banned
  15. # then it will refresh when the ban is lifted
  16. $refreshRate = 60;
  17.  
  18. # The initial limit of requests for ever refresh period
  19. $requestLimit = 60;
  20.  
  21. # The request limit for if the user is already banned and then exceeds this limit it will
  22. # add on the extreme ban time to the current ban time
  23. $extremeRequestLimit = 100;
  24.  
  25. # The initial amount of time to ban a user when the user exceeds the request limit
  26. $banTime = 30;
  27.  
  28. # The limit (seconds) to add on to the temporary ban time if the user exceeds the
  29. # extreme request limit
  30. $extremeBanTime = 560;
  31.  
  32.  
  33. /* * * * * * * *
  34.  * THE SCRIPT  *
  35.  * * * * * * * */
  36.  
  37. # Make sure session variables are set
  38. if(empty($_SESSION['lastaccess'])) {
  39.     $_SESSION['lastaccess'] = time();
  40.     $_SESSION['tempban']    = false;
  41.     $_SESSION['banlift']    = time();
  42.     $_SESSION['requests']   = 0;
  43. }
  44.  
  45. # Increase the request count by 1
  46. $_SESSION['requests'] = $_SESSION['requests'] + 1;
  47.  
  48. # If user is temporarily banned and requests have increased to the extreme request limit
  49. # then add $extremeBanTime to the ban time limit and throw HTTP/1.1 500 from now on until
  50. # ban is up.
  51. if($_SESSION['requests'] === $extremeRequestLimit) {
  52.     $_SESSION['banlift'] = time() + $extremeBanTime;
  53.     die();
  54. } elseif($_SESSION['tempban'] && $_SESSION['banlift'] > time() && $_SESSION['requests'] > $extremeRequestLimit) {
  55.     header('HTTP/1.1 500 Internal Server Error');
  56.     die();
  57. }
  58.  
  59. # If user is temporarily banned and the ban time has not passed then die()
  60. if($_SESSION['tempban'] && $_SESSION['banlift'] > time()) {
  61.     die(
  62.         '<p>Woops! Seems you have sent too many requests in too short of a time. Please wait 30 seconds until '.
  63.         @date("F j, Y, g:i:s a", $_SESSION['banlift']).
  64.         ' to access VoidRS Again.</p> '
  65.     );
  66. }
  67.  
  68. # If user is temporarily banned but the ban time is up then unban them and reset request count back to 0
  69. if($_SESSION['tempban'] && $_SESSION['banlift'] < time()) {
  70.     $_SESSION['requests'] = 0;
  71.     $_SESSION['tempban']  = false;
  72. }
  73.  
  74. # Reset the session every 60 seconds unless user is banned then waits
  75. if(($_SESSION['lastaccess'] - $refreshRate) > time()  && !$_SESSION['tempban']) {
  76.     $_SESSION['banlift']    = time();
  77.     $_SESSION['lastaccess'] = time();
  78.     $_SESSION['requests']   = 0;
  79. }
  80.  
  81. # if user exceeds the limit of requests for the given session refresh rate then ban them
  82. if($_SESSION['requests'] > $requestLimit && !$_SESSION['tempban']) {
  83.     $_SESSION['banlift'] = time() + $banTime;
  84.     $_SESSION['tempban'] = true;
  85. }
  86. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement