<?php

function rate_limit_check(string $key): void
{
    $limit = 5;
    $window = 300;

    $_SESSION['_rate'][$key] = $_SESSION['_rate'][$key] ?? [
        'count' => 0,
        'time' => time()
    ];

    $data = &$_SESSION['_rate'][$key];

    if (time() - $data['time'] > $window) {
        $data = ['count'=>0,'time'=>time()];
    }

    if ($data['count'] >= $limit) {
        http_response_code(429);
        exit('Too many attempts. Try later.');
    }
}

function rate_limit_fail(string $key): void
{
    $_SESSION['_rate'][$key]['count']++;
}
