Skip to content

Instantly share code, notes, and snippets.

@AlexanderPoellmann
Last active February 16, 2017 09:35
Show Gist options
  • Save AlexanderPoellmann/04539fa5802cafcac9dd5da9b258ebba to your computer and use it in GitHub Desktop.
Save AlexanderPoellmann/04539fa5802cafcac9dd5da9b258ebba to your computer and use it in GitHub Desktop.
Prevent multiple posting.
/**
* 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