Webbserver - Love Blomberg

Show sourcecode

The following files exists in this folder. Click to view.

public_html/GYA2/api/

admin_data.php
complete_session.php
export_csv.php
save_maze.php
save_reaction.php
save_simon.php
start_session.php

save_reaction.php

51 lines ASCII Unix (LF)
<?php
require __DIR__ '/../config.php';

header('Content-Type: application/json');

if (
$_SERVER['REQUEST_METHOD'] !== 'POST') {
    
http_response_code(405);
    echo 
json_encode(['error' => 'Method not allowed']);
    exit;
}

$data json_decode(file_get_contents('php://input'), true);

if (!
$data) {
    
http_response_code(400);
    echo 
json_encode(['error' => 'Invalid JSON']);
    exit;
}

$pid      = (int)($data['participant_id'] ?? 0);
$round    = (int)($data['round_number'] ?? 0);
$ms       = (int)($data['reaction_ms'] ?? 0);
$premature = (int)($data['was_premature'] ?? 0);
$delay    = (int)($data['delay_ms'] ?? 0);

if (
$pid <= || $round <= || $delay <= 0) {
    
http_response_code(400);
    echo 
json_encode(['error' => 'Invalid parameters']);
    exit;
}

try {
    
$pdo getDB();
    
$stmt $pdo->prepare(
        
'INSERT INTO reaction_times (participant_id, round_number, reaction_ms, was_premature, delay_ms)
         VALUES (:pid, :round, :ms, :premature, :delay)'
    
);
    
$stmt->execute([
        
':pid'       => $pid,
        
':round'     => $round,
        
':ms'        => $ms,
        
':premature' => $premature,
        
':delay'     => $delay,
    ]);

    echo 
json_encode(['success' => true]);
} catch (
PDOException $e) {
    
http_response_code(500);
    echo 
json_encode(['error' => 'Database error']);
}