-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.php
More file actions
32 lines (25 loc) · 811 Bytes
/
Copy pathdelete.php
File metadata and controls
32 lines (25 loc) · 811 Bytes
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
<?php
require "database.php";
session_start();
if (!isset($_SESSION["user"])) {
header("Location: login.php");
return;
}
$statement = $conn->prepare("SELECT * FROM contacts WHERE id = :id LIMIT 1");
$statement->execute([":id" => $_GET["id"]]);
if ($statement->rowCount() == 0) {
http_response_code(404);
echo("HTTP 404 NOT FOUND\n");
return;
}
$contact = $statement->fetch(PDO::FETCH_ASSOC);
/**< Does not allow user with different ID to delete a contact */
if ($contact["user_id"] !== $_SESSION["user"]["id"]) {
http_response_code(403);
echo("HTTP 403 UNAUTHORIZED");
return;
}
$statement = $conn->prepare("DELETE FROM contacts WHERE id = :id")->execute([":id" => $_GET["id"]]);
$_SESSION["flash"] = ["message" => "Contact {$contact['name']} deleted."];
header("Location: home.php");
?>