
تحديد معدل أفضل لطلبات PHP باستخدام Redis.
مكتبة Limitrr PHP مستوحاة بشكل كبير من مكتبتي الأخرى، Limitrr، التي أُنشئت لـ NodeJS. اطّلع عليها هنا
تتيح مكتبة Limitrr PHP للمستخدمين دمج تحديد معدل الطلبات بسهولة داخل تطبيقاتهم. وعلى عكس الحزم المماثلة الأخرى، تتيح هذه الأداة للمستخدم تحديد الحد ليس فقط حسب عدد الطلبات، بل أيضًا حسب عدد الإجراءات المكتملة (مثل السماح بإنشاء عدد معين من الحسابات بنجاح خلال فترة زمنية)، مع إمكانية تقييد ذلك بخيارات مخصصة. بالإضافة إلى ذلك، يمكن استخدام مميّزات مخصصة — لم يعد عليك الاقتصار على تحديد الحد حسب عنوان IP الخاص بالمستخدم.
توفر هذه المكتبة أيضًا دالة وسيطة (middleware) لتحديد معدل الطلبات بسهولة على المسارات المختلفة التي قد تكون لديك في مشروع SlimPHP.
إذا أعجبك هذا المشروع، يُرجى وضع 🌟 عليه على GitHub.
طلبات السحب (Pull Requests) مرحَّب بها
يمكنك تثبيت مكتبة limitrr-php بتنفيذ الأمر التالي في الطرفية (بافتراض أن لديك composer مثبّتًا)
composer require eddiejibson/limitrr-php "^1.0"
require "/vendor/autoload.php"; //Require composer's autoload
$options = [
//Redis keystore information
"redis" => [
"host" => "666.chae.sh",
"port" => 6379,
"password" => "supersecret",
],
"routes" => [
"default" => [
"requestsPerExpiry" => 5,
],
],
];
//Initialize the Limitrr class and pass the options defined above into it
//Note that the options are not required.
$limitrr = new \eddiejibson\limitrr\Limitrr($options);
//Various examples like this can be found further into the documentation,
//for each function.
$result = $limitrr->get(["discriminator" => $ip]);
echo $result["requests"] + " Requests";
echo $result["completed"] + " Completed";
//Note that this library is no means just for SlimPHP, it just happens to
//provide a middleware function for those who may need it.
//Usage within SlimPHP
$app = new Slim\App();
//Use the Limitrr SlimPHP middleware function, if you wish:
$app->add(new \eddiejibson\limitrr\RateLimitMiddleware($limitrr)); //Make sure to pass in the main Limitrr
//instance we defined above into the middleware function. This is mandatory.
//You can also add the get IP middleware function, it will append the user's real IP
//(behind Cloudflare or not) to the request.
$app->add(new \eddiejibson\limitrr\getIpMiddleware());
//Example usage within a route
$app->get("/hello/{name}", function ($request, $response, $args) {
$name = $args["name"];
$ip = $request->getAttribute('realip'); //Get the IP that was defined within Limitrr's get IP middleware function
return $response->getBody()->write("Hello, ${name}. Your IP is ${ip}.");
});
//You do not have to app the middleware function to every single route, globally.
//You can do it indivually, too - along with passing options into such. Like so:
$app->get("/createUser/{name}", function ($request, $response, $args) {
//Non intensive actions like simple verification will have a different limit to intensive ones.
//and will only be measured in terms of each request via the middleware.
//No further action is required.
if (strlen($args["name"]) < 5) {
//Dummy function creating user
$res = $someRandomClass->registerUser();
if ($res) {
//Intensive actions like actually registering a user should have a
//different limit to normal requests, hence the completedActionsPerExpiry option.
//and should only be added to once this task has been completed fully
//In this example, we will be limiting the amount of completed actions a certain IP can make.
//Anything can be passed in here, however. For example, a email address or user ID.
//$request->getAttribute('realip') was determined by calling the middleware earlier - getIpMiddleware()
$limitrr->complete(["discriminator"] => $ip);
}
}
})->add(new \eddiejibson\limitrr\RateLimitMiddleware($limitrr, ["route"=>"createUser"]));
//You can also pass the route name within the limitrr middleware function
$app->run();
الإرجاع: Array
$limitrr->get([
"discriminator" => $discriminator, //Required
"route" => $route, //Not required, default is assumed
"type" => $type //Not required
]);
يجب تمريرها إلى الدالة عبر مصفوفة (array)
defaultrequests أو completed في هذا المفتاح وسيتم عندها إرجاع القيمة المطلوبة فقط كعدد صحيح.$limitrr->get([
"discriminator" => $discriminator,
"type" => $type,
"route" => $route
]); //Besides discriminator, all parameters are optional.
//If type is not passed into the function, it will
//return both the amount of requests and completed actions
//Where discriminator is the thing being limited
//e.g x amount of completed actions/requests per discriminator
$limitrr->get(["discriminator" => $discriminator]);
//This tends to be the user's IP.
$limitrr->get(["discriminator" => $ip]);
//This will return both the amount of requests and completed actions stored under the
//discriminator provided in an object. You can handle like this:
$result = $limitrr->get(["discriminator" => $ip]);
echo $result["requests"] + " Requests";
echo $result["completed"] + " Completed";
//The above example would get the request and completed task count from the default
//route. If you would like to retrieve values from a different route, you can specify
//this as well. It can be done like this:
$result = $limitrr->get(["discriminator" => $ip, "route" => "exampleRouteName"]);
echo $result["requests"] . " Requests made through the route exampleRouteName";
echo $result["completed"] . " Completed Tasks made through the route exampleRouteName";
//You may also only fetch only one type of value - instead of both requests and completed.
$result = $limitrr->get(["discriminator" => $ip, "route" => "exampleRouteName", "type" => "completed"]);
echo $result["completed"] . " Completed tasks made through the route exampleRouteName";
الإرجاع: Integer
$limitrr->get([
"discriminator" => $discriminator, //Required
"route" => $route, //Not required, default is assumed
]);
يجب تمريرها إلى الدالة عبر مصفوفة (array)
defaultالإرجاع: Boolean