![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/soundstudiopro.com/private_html/ |
<?php
session_start();
require_once 'config/database.php';
header('Content-Type: application/json');
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
echo json_encode(['success' => false, 'error' => 'User not authenticated']);
exit;
}
// Check if request method is POST
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode(['success' => false, 'error' => 'Invalid request method']);
exit;
}
// Get JSON input
$input = json_decode(file_get_contents('php://input'), true);
$user_id = $input['user_id'] ?? null;
if (!$user_id) {
echo json_encode(['success' => false, 'error' => 'User ID is required']);
exit;
}
// Prevent self-following
if ($user_id == $_SESSION['user_id']) {
echo json_encode(['success' => false, 'error' => 'Cannot follow yourself']);
exit;
}
try {
$pdo = getDBConnection();
// Check if user already follows this user
$stmt = $pdo->prepare("SELECT id FROM user_follows WHERE follower_id = ? AND following_id = ?");
$stmt->execute([$_SESSION['user_id'], $user_id]);
$existing_follow = $stmt->fetch();
if ($existing_follow) {
// Remove follow
$stmt = $pdo->prepare("DELETE FROM user_follows WHERE follower_id = ? AND following_id = ?");
$stmt->execute([$_SESSION['user_id'], $user_id]);
echo json_encode(['success' => true, 'action' => 'unfollowed']);
} else {
// Add follow
$stmt = $pdo->prepare("INSERT INTO user_follows (follower_id, following_id, created_at) VALUES (?, ?, NOW())");
$stmt->execute([$_SESSION['user_id'], $user_id]);
echo json_encode(['success' => true, 'action' => 'followed']);
}
} catch (Exception $e) {
echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]);
}
?>