Created
July 20, 2012 16:11
-
-
Save jodeleeuw/3151606 to your computer and use it in GitHub Desktop.
PHP script to check if a turk worker has done an experiment
This file contains 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 | |
// sample usage in javascript: | |
/* | |
$.ajax({ | |
type: 'post', | |
url: '../exclude_subjects.php', | |
data: {"mturk_id": turk.workerId, "tables": JSON.stringify(['exp_1', 'exp_2']), "col_id": "mturk_id"}, | |
success: function(data) | |
{ | |
if(data=="1") | |
{ | |
// do stuff here to allow subject to continue | |
} else { | |
// subject already in database. do stuff to prevent them from doing experiment, like... | |
$("#instructions").html('<h3>Sorry, you have already participated in this study or a related study and are not eligible to complete this HIT.</h3>'); | |
} | |
} | |
}); | |
*/ | |
// edit this line for database check | |
include('database_connect.php'); | |
function check_mturk_id($tables, $id) { | |
for($i=0; $i < count($tables); $i++){ | |
$result = mysql_query('SELECT DISTINCT '.mysql_real_escape_string($col_id).' FROM '.mysql_real_escape_string($tables[$i])); | |
while($row = mysql_fetch_array($result)) | |
{ | |
if($row["mturk_id"] == $id) | |
{ | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
// get id from POST call | |
$id = $_POST['mturk_id']; | |
$col_id = $_POST['col_id']; | |
$tables = $_POST['tables']; | |
$tables = json_decode($tables,true); | |
// check id | |
$r = check_mturk_id($tables, $id, $col_id); | |
// echo out TRUE or FALSE | |
echo $r; | |
?> |
need a file database_connect.php with database connection info.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This should sanitize the variables that come in on POST to prevent injections... use at your own risk until that's fixed.