:: **Zploit** v1.0 | Current Path: **/home/kreativepixelz/soulmeetvivah.com/member/php/**
:: Editing File: track_profile_view.php
<?php session_start(); include('db.php'); header('Content-Type: application/json'); // Check database connection if (!$conn || $conn->connect_error) { echo json_encode(['status' => 'error', 'message' => 'Database connection failed']); exit; } $user_id = isset($_POST['profile_id']) ? intval($_POST['profile_id']) : (isset($_GET['profile_id']) ? intval($_GET['profile_id']) : 0); $viewer_id = isset($_SESSION['user_id']) ? intval($_SESSION['user_id']) : 0; // Validate inputs if ($viewer_id <= 0) { echo json_encode(['status' => 'error', 'message' => 'User not logged in']); exit; } if ($user_id <= 0) { echo json_encode(['status' => 'error', 'message' => 'Invalid profile ID']); exit; } if ($viewer_id == $user_id) { echo json_encode(['status' => 'success', 'message' => 'Self view - not tracked']); exit; } // First, ensure the table exists $table_check = $conn->query("SHOW TABLES LIKE 'recent_visitors'"); if (!$table_check || $table_check->num_rows == 0) { // Table doesn't exist - create it $create_table_sql = "CREATE TABLE IF NOT EXISTS `recent_visitors` ( `id` int(11) NOT NULL AUTO_INCREMENT, `profile_id` int(11) NOT NULL COMMENT 'User whose profile was viewed', `visitor_id` int(11) NOT NULL COMMENT 'User who viewed the profile', `seen_status` tinyint(1) DEFAULT 0 COMMENT '0 = not seen, 1 = seen by profile owner', `visited_at` datetime DEFAULT CURRENT_TIMESTAMP COMMENT 'When the profile was viewed', PRIMARY KEY (`id`), KEY `profile_id` (`profile_id`), KEY `visitor_id` (`visitor_id`), KEY `seen_status` (`seen_status`), KEY `visited_at` (`visited_at`), KEY `profile_visitor_date` (`profile_id`, `visitor_id`, `visited_at`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Tracks profile views/visitors'"; if (!$conn->query($create_table_sql)) { echo json_encode(['status' => 'error', 'message' => 'Failed to create table: ' . $conn->error]); exit; } } // Use prepared statement to prevent SQL injection $stmt_check = $conn->prepare("SELECT id FROM recent_visitors WHERE profile_id = ? AND visitor_id = ? AND visited_at > DATE_SUB(NOW(), INTERVAL 1 HOUR) LIMIT 1"); $stmt_check->bind_param("ii", $user_id, $viewer_id); $stmt_check->execute(); $check_result = $stmt_check->get_result(); if ($check_result && $check_result->num_rows > 0) { // Update the visit time if visited within last hour $row = $check_result->fetch_assoc(); $stmt_update = $conn->prepare("UPDATE recent_visitors SET visited_at = NOW() WHERE id = ?"); $stmt_update->bind_param("i", $row['id']); if ($stmt_update->execute()) { echo json_encode(['status' => 'success', 'message' => 'Visit updated']); } else { echo json_encode(['status' => 'error', 'message' => 'Failed to update: ' . $conn->error]); } $stmt_update->close(); } else { // Insert new visit record $stmt_insert = $conn->prepare("INSERT INTO recent_visitors (profile_id, visitor_id, seen_status, visited_at) VALUES (?, ?, 0, NOW())"); $stmt_insert->bind_param("ii", $user_id, $viewer_id); if ($stmt_insert->execute()) { $insert_id = $conn->insert_id; echo json_encode(['status' => 'success', 'message' => 'Visit tracked', 'insert_id' => $insert_id]); } else { $error_msg = $conn->error; error_log("Recent visitors insert error - Profile: $user_id, Viewer: $viewer_id, Error: $error_msg"); echo json_encode(['status' => 'error', 'message' => 'Failed to insert: ' . $error_msg]); } $stmt_insert->close(); } $stmt_check->close(); $conn->close(); ?>