Added “showrem” parameter to make it possible to monitor remaining hits

This commit is contained in:
pezcurrel 2024-10-31 20:24:29 +01:00
parent 30c314948c
commit c7eacfb643

View file

@ -1,6 +1,6 @@
<?php
function ckratelimit($headers) {
function ckratelimit($headers,$showrem=false) {
$aaheaders=[];
array_shift($headers);
foreach ($headers as $header)
@ -13,6 +13,7 @@ function ckratelimit($headers) {
if (!isset($aaheaders['x-ratelimit-remaining'])) return ['ok'=>false,'error'=>'no «x-ratelimit-remaining» header'];
if (preg_match('#^\d+$#',$aaheaders['x-ratelimit-remaining'])!==1) return ['ok'=>false,'error'=>'«x-ratelimit-remaining» header is not an integer'];
$remaining=$aaheaders['x-ratelimit-remaining']+0;
if ($showrem) echo "ckratelimit: remaining: {$remaining}\n";
$date=@strtotime($aaheaders['date']);
if (!is_int($date)) return ['ok'=>false,'error'=>'«date» header could not be converted to a unix timestamp'];
$reset=@strtotime($aaheaders['x-ratelimit-reset']);