-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle_like.php
More file actions
46 lines (36 loc) · 1.19 KB
/
toggle_like.php
File metadata and controls
46 lines (36 loc) · 1.19 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
<?php
session_start();
require_once 'db/config.php';
require_once 'db/blog_functions.php';
header('Content-Type: application/json');
// Check if it's a POST request
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['success' => false, 'error' => 'Method not allowed']);
exit;
}
// Get JSON input
$input = json_decode(file_get_contents('php://input'), true);
if (!isset($input['blog_id'])) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => 'Blog ID is required']);
exit;
}
$blogId = intval($input['blog_id']);
// Check if user is logged in (assuming session variables exist)
$userId = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;
$expertId = isset($_SESSION['expert_id']) ? $_SESSION['expert_id'] : null;
if (!$userId && !$expertId) {
http_response_code(401);
echo json_encode(['success' => false, 'error' => 'User not logged in']);
exit;
}
// Toggle the like
$result = toggleBlogLike($conn, $blogId, $userId, $expertId);
if ($result['success']) {
// Get updated like count
$likeCount = getBlogLikeCount($conn, $blogId);
$result['like_count'] = $likeCount;
}
echo json_encode($result);
?>