-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_message.php
More file actions
50 lines (43 loc) · 1.52 KB
/
Copy pathdelete_message.php
File metadata and controls
50 lines (43 loc) · 1.52 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
<?php
session_start();
include 'includes/db_connect.php';
// Check login
if (!isset($_SESSION['user_id'])) {
die("unauthorized");
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['message_id'])) {
$message_id = intval($_POST['message_id']);
$user_id = intval($_SESSION['user_id']);
// 1️⃣ Fetch message details
$stmt = $conn->prepare("SELECT sender_id, image_path FROM chat_messages WHERE id = ?");
$stmt->bind_param("i", $message_id);
$stmt->execute();
$result = $stmt->get_result();
$message = $result->fetch_assoc();
$stmt->close();
// 2️⃣ Debug check — is the message found?
if (!$message) {
die("not_found");
}
// 3️⃣ Compare user ID with sender ID
if ((int)$message['sender_id'] === (int)$user_id) {
// 4️⃣ If message had an image, delete it from server
if (!empty($message['image_path'])) {
$filePath = $message['image_path'];
if (file_exists($filePath)) {
unlink($filePath);
} elseif (file_exists("uploads/chat_images/" . $filePath)) {
unlink("uploads/chat_images/" . $filePath);
}
}
// 5️⃣ Delete message from database
$delete = $conn->prepare("DELETE FROM chat_messages WHERE id = ?");
$delete->bind_param("i", $message_id);
$delete->execute();
$delete->close();
echo "success";
} else {
echo "unauthorized";
}
}
?>