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

start_session.php

54 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;
}

$email trim($data['email'] ?? '');
$group $data['group_type'] ?? '';
$userAgent $data['user_agent'] ?? '';

if (!
$email || !filter_var($emailFILTER_VALIDATE_EMAIL)) {
    
http_response_code(400);
    echo 
json_encode(['error' => 'Invalid email']);
    exit;
}

if (!
in_array($group, ['placebo''control'], true)) {
    
http_response_code(400);
    echo 
json_encode(['error' => 'Invalid group type']);
    exit;
}

try {
    
$pdo getDB();
    
$stmt $pdo->prepare(
        
'INSERT INTO participants (email, group_type, user_agent, ip_address)
         VALUES (:email, :group_type, :user_agent, :ip)'
    
);
    
$stmt->execute([
        
':email'      => $email,
        
':group_type' => $group,
        
':user_agent' => $userAgent,
        
':ip'         => $_SERVER['REMOTE_ADDR'] ?? '',
    ]);

    echo 
json_encode(['participant_id' => (int)$pdo->lastInsertId()]);
} catch (
PDOException $e) {
    
http_response_code(500);
    echo 
json_encode(['error' => 'Database error']);
}