forked from mathiasbynens/php-url-shortener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
59 lines (47 loc) · 1.35 KB
/
index.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
<?php
require('config.php');
function redirect($url) {
header('Location: ' . $url, null, 301);
die();
}
if (isset($_GET['slug'])) {
$slug = rtrim($_GET['slug'], '!"#$%&\'()*+,-./@:;<=>[\\]^_`{|}~');
switch(DB_FLAVOR) {
case "mysql":
$dsn = DB_FLAVOR . ":dbname=" . DB_DATABASE . ";host=" . DB_HOST;
break;
case "sqlite":
$dsn = DB_FLAVOR . ":" . DB_DATABASE;
break;
default:
exit("Unsupported database.");
break;
}
try {
$db = new PDO($dsn, DB_USER, DB_PASSWORD);
} catch (PDOException $e) {
exit("Database connection error: " . $e->getMessage(). "\n");
}
if (DB_FLAVOR == "sqlite") {
$row = $db->query("select name from sqlite_master where type = 'table' and name = 'redirect'")->fetch();
if ( ! $row) {
exit("Url not found.");
}
}
if (DB_FLAVOR == "mysql") $db->query('SET NAMES "utf8"');
$lookup_stmt = $db->prepare('SELECT `url` FROM `redirect` WHERE `slug` = :slug');
$lookup_stmt->bindParam(':slug', $slug);
$lookup_stmt->execute();
$result = $lookup_stmt->fetch();
if ($result) {
$update_stmt = $db->prepare('UPDATE `redirect` SET `hits` = `hits` + 1 WHERE `slug` = :slug');
$update_stmt->bindParam(':slug', $slug);
$update_stmt->execute();
redirect($result['url']);
} else {
redirect(DEFAULT_URL . $_SERVER['REQUEST_URI']);
}
} else {
redirect(DEFAULT_URL . '/');
}
?>