Last active
February 16, 2017 09:35
-
-
Save AlexanderPoellmann/04539fa5802cafcac9dd5da9b258ebba to your computer and use it in GitHub Desktop.
Prevent multiple posting.
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
/** | |
* Prevent multiple posting. | |
* | |
* @return bool | |
*/ | |
public function antiMultiPosting() { | |
/** | |
* @var $user User | |
*/ | |
$user = auth()->user(); | |
// permission error | |
if (! $user) | |
return false; | |
// anti multi-posting token | |
$token = strtolower('_last_creation_'. $user->username); | |
// if token isn't set, ensure $allow will always be true | |
$last = session()->get($token, -1); | |
$time = microtime(true); | |
// calculate difference | |
$allow = (($time - $last) > 10); | |
// check if we're multi-posting | |
if (! $allow) { | |
$log = []; | |
$log[] = $user->id; | |
$log[] = $user->username; | |
$log[] = '('. ($time - $last) .' seconds between attempts)'; | |
logger()->warning('[Anti Multi-Posting] '. implode(', ', $log)); | |
return false; | |
} | |
// update anti multi-posting token | |
// use actual microtime here, not the already older $time variable | |
session()->put($token, microtime(true)); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment