Skip to content

Instantly share code, notes, and snippets.

@sbarre
Last active February 6, 2018 17:47
Show Gist options
  • Save sbarre/800ac36d9cb13bebac73fb4739eac7be to your computer and use it in GitHub Desktop.
Save sbarre/800ac36d9cb13bebac73fb4739eac7be to your computer and use it in GitHub Desktop.
Quick function to identify potentially duplicate votes
<?php
$votes = [];
$vote_hashes = [];
// time in seconds that must have elapsed between votes
$acceptable_time_delta = 120;
foreach ($voting_data as $row) {
$count_this_vote = true;
// generate a unique hash for this vote
$vote_hash = md5($row['user_agent'].$row['ip'].$row['value']);
// get the timestamp
$vote_timestamp = DateTime::createFromFormat('Y-m-d H:i:s', $row['date_created'])->getTimestamp();
// skip check if this is the first occurence of this vote
if (in_array($vote_hash,array_keys($vote_hashes))) {
// delta in seconds between this vote and the previous vote for this hash
$delta = $vote_timestamp - $vote_hashes[$vote_hash];
// don't count the vote if it's too soon
if ($delta < $acceptable_time_delta) {
$count_this_vote = false;
}
}
// store our vote in our global hashes array
$vote_hashes[$vote_hash] = $vote_timestamp;
if ($count_this_vote) {
$votes[$row['value']] += 1;
}
}
print_r($votes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment