-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
75 lines (65 loc) · 1.63 KB
/
functions.php
File metadata and controls
75 lines (65 loc) · 1.63 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
include('config.php');
function generateRandomAlias()
{
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$randomAlias = '';
for ($i = 0; $i < 10; $i++) {
$randomIndex = rand(0, strlen($characters) - 1);
$randomAlias .= $characters[$randomIndex];
}
return $randomAlias;
}
function isAliasExists($alias)
{
global $conn;
$stmt = $conn->prepare("SELECT COUNT(*) FROM urls WHERE alias = ?");
$stmt->bind_param("s", $alias);
$stmt->execute();
$stmt->bind_result($count);
$stmt->fetch();
$stmt->close();
return $count > 0;
}
function generateUniqueAlias()
{
do {
$randomString = generateRandomAlias();
} while (isAliasExists($randomString));
return $randomString;
}
function addAlias($url, $vanityUrl)
{
global $conn;
$stmt = $conn->prepare("INSERT INTO urls (alias,url) VALUES (?,?)");
$stmt->bind_param("ss", $vanityUrl, $url);
$stmt->execute();
}
function isUrlExists($url)
{
global $conn;
$stmt = $conn->prepare("SELECT alias FROM urls WHERE url = ? LIMIT 1");
$stmt->bind_param("s", $url);
$stmt->execute();
$result = $stmt->get_result();
if ($row = $result->fetch_assoc()) {
return $row['alias'];
}
return false;
}
function redirect($str)
{
global $conn;
$stmt = $conn->prepare("SELECT url FROM urls where alias =?");
$stmt->bind_param("s", $str);
$stmt->execute();
$stmt->bind_result($url);
$stmt->fetch();
if (!empty($url)) {
echo "Redirecting...";
header("Location: $url");
exit;
} else {
echo "Invalid Url";
}
}