Code Monkey home page Code Monkey logo

ratelimiter's Introduction

ratelimiter

Simple and effective rate limiter using PHP and MySQL

usage

include("ratelimiter.php");
ratelimiter("255.255.255.255", "myservice", 5, "10 MINUTE"); // -> bool

If the returned value is true, you are not rate limited. Every time that you call this function, the requests column in the database will increment for the given service. Example use:

include("ratelimiter.php");
if (!ratelimiter($_SERVER["REMOTE_ADDR"], "myservice", 5, "10 MINUTE")) exit("You are being rate limited");
else /* do stuff */;

Please make sure to check LICENSE before using. Commercial use is prohibited.

installation

Clone the repository in your desired folder.

git clone https://github.com/GSRV-io/ratelimiter.git .

Run the following SQL query to initialise the table.

CREATE TABLE `ratelimits` (
  `ip` varchar(256) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
  `service` varchar(128) NOT NULL DEFAULT 'general',
  `requests` int(11) NOT NULL DEFAULT 1,
  `time` datetime NOT NULL DEFAULT utc_timestamp()
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

The code looks for a variable named $conn that defines a MySQL connection. The table must be available within this connection. To use a variable with a different name, replace all occurrances of $conn in ratelimiter.php with your variable.

ratelimiter's People

Contributors

zefir-git avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.