Skip to content

Instantly share code, notes, and snippets.

@nicolasdanelon
Last active July 14, 2023 16:14
Show Gist options
  • Save nicolasdanelon/951abc41ff5a73b4ccad9f8b2fbd3495 to your computer and use it in GitHub Desktop.
Save nicolasdanelon/951abc41ff5a73b4ccad9f8b2fbd3495 to your computer and use it in GitHub Desktop.
Simple php 'server' with CORS for react/preact & vite project
<?php
// $ php -S 127.0.0.1:9001
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: http://localhost:5173'); // CORS
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
$json = file_get_contents('php://input');
$data = json_decode($json, true);
if (isset($data['username']) && isset($data['password'])) {
if ($data['username'] === 'nicolas' && $data['password'] === 'nicolasm') {
echo json_encode(['status' => 'success', 'token' => 'a9ace8e1919b2c1e7eba8e801ef58971']);
} else {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'Invalid username or password']);
}
} else {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'Username or password not provided']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment