forked from mathiasbynens/php-url-shortener
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
45 lines (31 loc) · 1.2 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
<?php
require 'config.php';
$url = DEFAULT_URL . '/';
if (isset($_GET['slug'])) {
$slug = $_GET['slug'];
if ('@' == $slug) {
$url = 'http://twitter.com/' . TWITTER_USERNAME;
} else if (' ' == $slug) { // +
$url = 'https://plus.google.com/u/0/' . GOOGLE_PLUS_ID . '/posts';
} else {
$slug = preg_replace('/[^a-z0-9]/si', '', $slug);
if (is_numeric($slug) && strlen($slug) > 8) {
$url = 'http://twitter.com/' . TWITTER_USERNAME . '/status/' . $slug;
} else {
$db = new MySQLi(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
$db->set_charset('utf8');
$escapedSlug = $db->real_escape_string($slug);
$redirectResult = $db->query('SELECT url FROM redirect WHERE slug = "' . $escapedSlug . '"');
if ($redirectResult && $redirectResult->num_rows > 0) {
$db->query('UPDATE redirect SET hits = hits + 1 WHERE slug = "' . $escapedSlug . '"');
$url = $redirectResult->fetch_object()->url;
} else {
$url = DEFAULT_URL . $_SERVER['REQUEST_URI'];
}
$db->close();
}
}
}
header('Location: ' . $url, null, 301);
?>
<meta http-equiv=refresh content="0;URL=<?php echo $url; ?>"><a href="<?php echo $url; ?>">Continue</a><script>location.href='<?php echo $url; ?>'</script>