Created
January 14, 2014 22:22
-
-
Save TGM/8427084 to your computer and use it in GitHub Desktop.
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 | |
/* Example PHP Callback Script, PDO version */ | |
/* ExtremeTop does not take responsibility for the use of this PHP snippet */ | |
/* Validate post request and sanitize accordantly */ | |
if (!empty($_POST['userid'])) | |
{ | |
$userID = filter_var($_POST['userid'], FILTER_VALIDATE_INT, array('options' => array('default' => 0, "min_range" => 0))); | |
} | |
else | |
{ | |
exit; | |
} | |
if ($userID) | |
{ | |
/* Define vote cooldown in hours */ | |
$cooldown = 12; | |
/* Connect to database */ | |
$db = new PDO('mysql:host=HOSTNAME;dbname=DATABASE;charset=utf8', 'USERNAME', 'PASSWORD'); | |
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); | |
/* Check if the this user exists in our database */ | |
$stmt = $db->prepare('SELECT COUNT(*) as counter FROM vote_callback WHERE userID = :userID LIMIT 1'); | |
$stmt->execute(array( ':userID' => $userID )); | |
$rows = $stmt->fetch(PDO::FETCH_ASSOC); | |
/* First time voting? Welcome */ | |
if (!$rows['counter']) | |
{ | |
/* Register him for further use */ | |
$stmt = $db->prepare('INSERT INTO vote_callback SET userID = :userID, last_vote = NOW(), points = 1'); | |
$stmt->execute(array(':userID' => $userID)); | |
} | |
elseif ($rows['counter']) | |
{ | |
/* Reward user with +1 point */ | |
$stmt = $db->prepare('UPDATE vote_callback SET points = points + 1, last_vote = NOW() | |
WHERE userID = :userID AND last_vote <= NOW() - INTERVAL :cooldown HOUR'); | |
$stmt->execute(array(':userID' => $userID, ':cooldown' => $cooldown)); | |
} | |
} | |
/* Table Example */ | |
/** | |
DROP TABLE IF EXISTS `vote_callback`; | |
CREATE TABLE `vote_callback` ( | |
`userID` int(10) unsigned NOT NULL, | |
`last_vote` timestamp NULL DEFAULT NULL, | |
`points` smallint(5) unsigned NOT NULL DEFAULT '0' | |
) TYPE=InnoDB; | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment