-
Notifications
You must be signed in to change notification settings - Fork 0
/
geturl.php
66 lines (58 loc) · 1.98 KB
/
geturl.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
63
64
65
66
<?php
require_once "vendor/autoload.php";
use Dotenv\Dotenv;
function connectdb() {
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
$dbHost = $_ENV['DB_HOST'];
$dbName = $_ENV['DB_NAME'];
$dbUser = $_ENV['DB_USER'];
$dbPass = $_ENV['DB_PASS'];
if (!$dbHost || !$dbName || !$dbUser) {
echo "Database configuration error!";
die();
}
try {
$db = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUser, $dbPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $db;
} catch (PDOException $e) {
echo "Database connection error: " . $e->getMessage();
die();
}
}
function getlink($shortedlink) {
$db = connectdb();
$stmt = $db->prepare("SELECT links FROM shortedlinks WHERE shortedid = :shortedid");
$stmt->bindParam(':shortedid', $shortedlink);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if ($result) {
return $result['links'];
} else {
return null;
}
}
$shortedlink = $_GET['urlshorted'];
$getlink = getlink($shortedlink);
if ($getlink) {
header("Location: $getlink");
exit();
} else {
include_once "index.php";
echo '
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/sweetalert2.all.min.js"></script>
<script>
Swal.fire({
icon: "error",
title: "Shortlink not found",
text: "We couldn\'t find the requested link in our system.",
confirmButtonColor: "#4299e1",
confirmButtonText: "OK, I will check again",
}).then(function() {
window.location.href = "/";
});
</script>
';
}
?>