Created
March 23, 2018 14:35
-
-
Save dovidezra/aee9e5dabf63369e4e9bc72c6641d777 to your computer and use it in GitHub Desktop.
Rate Limit in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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