0% found this document useful (0 votes)
23 views2 pages

Update Parking

The PHP script handles admin authentication and database connection for updating parking slot statuses. It checks if the request method is POST with a PUT override, updates the database if valid, and sends a WebSocket message if the status is 'reserved'. If any step fails, it returns an appropriate JSON error response.

Uploaded by

Blue Lions
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

Update Parking

The PHP script handles admin authentication and database connection for updating parking slot statuses. It checks if the request method is POST with a PUT override, updates the database if valid, and sends a WebSocket message if the status is 'reserved'. If any step fails, it returns an appropriate JSON error response.

Uploaded by

Blue Lions
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

<?

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

// 🔒 Admin auth check


if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
echo json_encode(['success' => false, 'error' => 'Unauthorized']);
exit();
}

// 🧠 Try connecting to the database


try {
$conn = new PDO("mysql:host=localhost;dbname=epark_db", "root", "", [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
} catch (PDOException $e) {
echo json_encode(['success' => false, 'error' => 'Database connection
failed']);
exit();
}

// ✅ WebSocket send function (raw)


function sendWebSocketData($host, $port, $path, $data) {
$key = base64_encode(random_bytes(16));
$header = "GET $path HTTP/1.1\r\n"
. "Host: $host:$port\r\n"
. "Upgrade: websocket\r\n"
. "Connection: Upgrade\r\n"
. "Sec-WebSocket-Key: $key\r\n"
. "Sec-WebSocket-Version: 13\r\n\r\n";

$socket = fsockopen($host, $port, $errno, $errstr, 5);

if (!$socket) {
return false;
}

fwrite($socket, $header);
fread($socket, 2000); // wait for server handshake

// Frame: 0x81 = FIN + text frame opcode, then payload length + data
$frame = chr(0x81) . chr(strlen($data)) . $data;

fwrite($socket, $frame);
fclose($socket);
return true;
}

// 🌱 If request is a PUT (via POST _method)


if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['_method']) &&
$_POST['_method'] === 'PUT') {
if (!empty($_POST['slot_number']) && !empty($_POST['status'])) {
$slot_number = $_POST['slot_number'];
$status = $_POST['status'];

// ✅ Update DB
$query = "UPDATE parking_slots SET status = :status WHERE slot_number
= :slot_number";
$stmt = $conn->prepare($query);

if ($stmt->execute(['status' => $status, 'slot_number' => $slot_number])) {

// 📡 Send to NodeMCU if reserved


if ($status === 'reserved') {
$host = '192.168.254.101'; // NodeMCU IP
$port = 80; // WebSocket port
$path = '/';
$data = "slot $slot_number $status";

if (sendWebSocketData($host, $port, $path, $data)) {


echo json_encode(['success' => true, 'message' => 'Sent via
WebSocket']);
} else {
echo json_encode(['success' => false, 'error' => 'WebSocket
connection failed']);
}

exit();
}

echo json_encode(['success' => true]);


exit();
} else {
echo json_encode(['success' => false, 'error' => 'Failed to update
database']);
exit();
}
}
}

echo json_encode(['success' => false, 'error' => 'Invalid request']);


exit();

You might also like