Voting Postback Integration
When a user votes on your server through ArenaTop100, you can track their votes and reward them using a unique player ID. Here's a step-by-step guide on how to set up and integrate our postback system.
Step 1: Set Up the Voting URL
Include your unique player ID in the voting URL to track the votes. The URL should be in the following format:
https://arenatop100.com/details/[SiteSlug]?userId=[UniquePlayerID]
- Replace
[SiteSlug]
with your site's slug from ArenaTop100. - Replace
[UniquePlayerID]
with the unique identifier you use to track your players (e.g., their username or player ID).
Example:
<a href="https://arenatop100.com/details/[SiteSlug]?userId=[UniquePlayerID]">
<img src="https://arenatop100.com/vote-banner.webp" alt="Vote for our server!" />
</a>
Step 2: Configure Your Postback URL
In your ArenaTop100 account settings, specify the URL where we will send the postback request after a successful vote. This URL should point to a script on your server that handles the postback data.
https://yourwebsite.com/postback-handler.php
Step 3: Implement the Postback Handler
Create a PHP script on your server to handle the postback request. This script will:
- Verify that the request comes from ArenaTop100.
- Validate the
userId
received in the POST request. - Credit the user for voting and update their voting status.
Example PHP Code:
1<?php
2// Include database connection
3include('db_connect.php'); // Ensure this file connects to the correct database
4
5// Check if request comes from arenatop100.com
6if (gethostbyname("arenatop100.com") !== $_SERVER['REMOTE_ADDR']) {
7 error_log("Unauthorized postback attempt from IP: " . $_SERVER['REMOTE_ADDR']);
8 echo json_encode(['success' => false, 'message' => 'Unauthorized request.']);
9 exit;
10}
11
12// Get the JSON payload from the POST request
13$input = file_get_contents("php://input");
14$data = json_decode($input, true);
15
16// Validate the input data
17if (!isset($data['success']) || !isset($data['userId'])) {
18 error_log("Invalid postback request: Missing required fields.");
19 echo json_encode(['success' => false, 'message' => 'Invalid request.']);
20 exit;
21}
22
23// Assign variables from the payload
24$success = $data['success'];
25$userId = filter_var($data['userId'], FILTER_SANITIZE_STRING);
26
27if (!$userId) {
28 // Log invalid userId and send an error response
29 error_log("Invalid postback request: missing or invalid userId.");
30 echo json_encode(['success' => false, 'message' => 'Invalid request.']);
31 exit;
32}
33
34if ($success) {
35 // Success case: The vote is valid
36 mysqli_begin_transaction($conn, MYSQLI_TRANS_START_READ_WRITE);
37
38 try {
39 // Credit the user for voting (e.g., 10 credits)
40 $creditUserQuery = "UPDATE users SET credits = credits + 10 WHERE user_id = ?";
41 $stmt = mysqli_prepare($conn, $creditUserQuery);
42 mysqli_stmt_bind_param($stmt, 's', $userId);
43 mysqli_stmt_execute($stmt);
44 mysqli_stmt_close($stmt);
45
46 // Update the voting status (if necessary)
47 $updateVoteStatusQuery = "UPDATE users SET vote = vote + 1 WHERE user_id = ?";
48 $stmt = mysqli_prepare($conn, $updateVoteStatusQuery);
49 mysqli_stmt_bind_param($stmt, 's', $userId);
50 mysqli_stmt_execute($stmt);
51 mysqli_stmt_close($stmt);
52
53 // Commit the transaction
54 mysqli_commit($conn);
55
56 // Respond to ArenaTop100 with success
57 echo json_encode([
58 'success' => true,
59 'message' => "User with userId '$userId' successfully voted and received 10 credits."
60 ]);
61 } catch (Exception $e) {
62 // Rollback the transaction on failure
63 mysqli_rollback($conn);
64 error_log("Error processing postback for userId $userId: " . $e->getMessage());
65 echo json_encode(['success' => false, 'message' => 'An error occurred.']);
66 } finally {
67 // Always close the database connection
68 mysqli_close($conn);
69 }
70} else {
71 // Failure case: The vote is invalid or user must wait
72 error_log("Vote failed for userId $userId: " . $data['message']);
73 echo json_encode([
74 'success' => false,
75 'message' => $data['message']
76 ]);
77}
78?>
How It Works
- User Clicks the Banner: The user clicks on the voting banner on your site, which redirects them to ArenaTop100 with their unique player ID in the URL.
- User Votes on ArenaTop100: The user votes for your server on ArenaTop100.
- Postback Request: After a successful vote, ArenaTop100 sends a POST request to your specified postback URL with the
userId
. - Handle Postback: Your PHP script processes the postback request, validates the
userId
, credits the user, updates their voting status and responds with a confirmation.
By following these steps, you can seamlessly integrate ArenaTop100's voting postback system and reward your players for voting.