-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreceive_url.php
55 lines (43 loc) · 1.83 KB
/
receive_url.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
<?php
// Include database configuration
include 'config_1.php';
// Create a new database connection
$conn = getDbConnection();
// Get POST data from incoming request
$data = json_decode(file_get_contents('php://input'), true);
// Validate received data before adding it to the database
if (isset($data['timestamp'], $data['url'], $data['valid'])) {
// Prepare and bind the SQL statement
$stmt = $conn->prepare("SELECT COUNT(*) FROM urls WHERE url = ?");
$urlWithoutTrailingSlash = rtrim($data['url'], '/'); // Ensure no trailing slashes when checking for duplicates.
// Bind parameters and execute the statement
$stmt->bind_param("s", $urlWithoutTrailingSlash);
$stmt->execute();
// Get the result
$stmt->bind_result($urlExistsCount);
$stmt->fetch();
// Close the prepared statement for checking existence
$stmt->close();
// If the URL does not exist, insert it into the database
if ($urlExistsCount == 0) {
// Prepare the insert statement
$stmt = $conn->prepare("INSERT INTO urls (timestamp, url, valid) VALUES (?, ?, ?)");
// Bind parameters
$stmt->bind_param("ssi", $data['timestamp'], $urlWithoutTrailingSlash, $data['valid']);
// Execute the insert statement
if ($stmt->execute()) {
echo json_encode(["status" => "success", "message" => "URL added successfully."]);
} else {
echo json_encode(["status" => "error", "message" => "Failed to add URL."]);
}
// Close the prepared statement for insertion
$stmt->close();
} else {
echo json_encode(["status" => "error", "message" => "URL already exists."]);
}
} else {
echo json_encode(["status" => "error", "message" => "Invalid data received."]);
}
// Close the database connection
$conn->close();
?>