-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_config.php
More file actions
58 lines (50 loc) · 1.59 KB
/
Copy pathapi_config.php
File metadata and controls
58 lines (50 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
// Configure session for cross-origin requests before starting the session
ini_set('session.cookie_samesite', 'None');
ini_set('session.cookie_secure', '1');
ini_set('session.cookie_httponly', '1');
ini_set('session.use_only_cookies', '1');
ini_set('session.cookie_path', '/');
// Start session after configuration
session_start();
// Enable CORS
header("Access-Control-Allow-Origin: http://localhost:4200"); // Angular dev server
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
header("Access-Control-Allow-Credentials: true");
header("Content-Type: application/json; charset=UTF-8");
// Handle preflight OPTIONS request
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit();
}
// Include database configuration
require_once 'config.php';
// Common response functions
function sendResponse($data, $status = 200) {
http_response_code($status);
echo json_encode($data);
exit();
}
function sendError($message, $status = 400) {
http_response_code($status);
echo json_encode(['error' => $message]);
exit();
}
// Check if user is authenticated
function isAuthenticated() {
if (!isset($_SESSION['user_id'])) {
sendError('Unauthorized', 401);
}
return $_SESSION['user_id'];
}
// Get request body as JSON
function getRequestBody() {
$json = file_get_contents('php://input');
$data = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
sendError('Invalid JSON data');
}
return $data;
}
?>