forked from hhlitval/simple-portfolio-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirect.php
More file actions
45 lines (36 loc) · 1.02 KB
/
Copy pathredirect.php
File metadata and controls
45 lines (36 loc) · 1.02 KB
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
<?php
require_once 'config/database.php';
$code = $_GET['code'] ?? '';
if (empty($code)) {
http_response_code(404);
echo "404 Not Found";
exit;
}
$database = new Database();
$db = $database->getConnection();
$stmt = $db->prepare("SELECT id, original_url FROM short_links WHERE short_code = ?");
$stmt->execute([$code]);
$link = $stmt->fetch(PDO::FETCH_ASSOC);
if ($link) {
$url = $link['original_url'];
// Validate URL to prevent open redirect
$parsed = parse_url($url);
if ($parsed === false || empty($parsed['host'])) {
$url = '/';
}
// Whitelist allowed schemes
$scheme = $parsed['scheme'] ?? '';
if (!in_array($scheme, ['http', 'https', ''])) {
$url = '/';
}
// Increment click count
$update = $db->prepare("UPDATE short_links SET click_count = click_count + 1 WHERE id = ?");
$update->execute([$link['id']]);
header("Location: " . $url);
exit;
} else {
http_response_code(404);
echo "404 Not Found - Link does not exist";
exit;
}
?>