Files
Colaborando/manejoDeSesiones.php
2026-05-26 00:59:40 -06:00

27 lines
1.1 KiB
PHP

<?php
$file = 'datos.json';
if (isset($_GET['nombre']) && $_GET['nombre'] != '') {
$nombre = basename($_GET['nombre']); // 'basename' ayuda a evitar que intenten acceder a carpetas superiores
// Si el nombre no termina en .json, se lo agregamos
if (pathinfo($nombre, PATHINFO_EXTENSION) != 'json') {
$nombre .= '.json';
}
$file = $nombre;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$data = json_decode(file_get_contents('php://input'), true);
file_put_contents($file, json_encode($data['data'], JSON_PRETTY_PRINT));
echo json_encode(['status' => 'success', 'data' => ['message' => 'Data saved successfully']]);
} elseif ($_SERVER['REQUEST_METHOD'] === 'GET') {
if (file_exists($file)) {
$data = file_get_contents($file);
echo json_encode(['status' => 'success', 'data' => json_decode($data)]);
} else {
echo json_encode(['status' => 'error', 'data' => ['message' => 'File not found']]);
}
} else {
echo json_encode(['status' => 'error', 'data' => ['message' => 'Invalid request method']]);
}