Skip to content

Instantly share code, notes, and snippets.

@dovidezra
Created March 23, 2018 14:35
Show Gist options
  • Save dovidezra/aee9e5dabf63369e4e9bc72c6641d777 to your computer and use it in GitHub Desktop.
Save dovidezra/aee9e5dabf63369e4e9bc72c6641d777 to your computer and use it in GitHub Desktop.
Rate Limit in PHP
<?php
session_start();
if (isset($_SESSION['LAST_CALL'])) {
$last = strtotime($_SESSION['LAST_CALL']);
$curr = strtotime(date("Y-m-d h:i:s"));
$sec = abs($last - $curr);
if ($sec <= 1) {
$data = 'Rate Limit Exceeded'; // rate limit
header('Content-Type: application/json');
die (json_encode($data));
}
}
$_SESSION['LAST_CALL'] = date("Y-m-d h:i:s");
// normal usage
$data = "Data Returned from API";
header('Content-Type: application/json');
die(json_encode($data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment