-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceive_stats.php
62 lines (50 loc) · 1.78 KB
/
receive_stats.php
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
51
52
53
54
55
56
57
58
59
60
61
62
<?php
// $servername = "localhost";
// $username = "root";
// $password = "";
// $dbname = "database00";
$servername = 'localhost';
$username = 'fgpooswu_stats_user';
$password = 'p4s5w0rd_com';
$dbname = 'fgpooswu_stats';
// Read the data sent from the JavaScript code
$requestData = json_decode(file_get_contents('php://input'), true);
// Extract the values from the data
$currentURL = $requestData['currentURL'];
$historyURL = $requestData['historyURL'];
$ipAddress = $requestData['ipAddress'];
$country = $requestData['country'];
// Create a connection to the database
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Function to increment the count column for a given URL
function incrementCount($conn, $table, $column, $value)
{
$sql = "SELECT * FROM $table WHERE $column = '$value'";
$result = $conn->query($sql);
if ($result && $result->num_rows > 0) {
$row = $result->fetch_assoc();
$count = $row['count'] + 1;
$sql = "UPDATE $table SET count = $count WHERE $column = '$value'";
$conn->query($sql);
} else {
$sql = "INSERT INTO $table ($column, count) VALUES ('$value', 1)";
$conn->query($sql);
}
}
// Increment the count for currentURL
incrementCount($conn, 'table1', 'url', $currentURL);
// Increment the count for historyURL
incrementCount($conn, 'table2', 'history', $historyURL);
// Increment the count for ipAddress
incrementCount($conn, 'table3', 'ip', $ipAddress);
// Increment the count for country
incrementCount($conn, 'table4', 'country', $country);
// Close the database connection
$conn->close();
// Send a response to indicate the success
http_response_code(200);
echo "Data received and processed successfully.";
?>