/
accessUrl.php
38 lines (30 loc) · 1.04 KB
/
accessUrl.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
<?php
require_once('includes/config.php');
require_once('includes/database.php');
require_once('includes/shortUrl.php');
// Create new connection to Database
$database = new GgdShortener_Database($config['database']);
// get request data
$path = substr($_SERVER['REQUEST_URI'], 1);
$httpMethod = filter_input(INPUT_SERVER, 'REQUEST_METHOD');
$url = NULL;
// if url has letters and digits, has a length of 5 and is GET then execute
if ((ctype_alnum($path)) && (strlen($path) == 5) && ("GET" == $httpMethod)) {
$sql = "SELECT * FROM short_url WHERE code = :code";
try {
$database->beginTransaction();
$q = $database->prepare($sql);
$q->bindValue(':code', $path, PDO::PARAM_STR);
$q->execute();
} catch (Exception $e) {
error_log("An error occured while getting data");
error_log($e->getMessage());
$database->rollBack();
return false;
}
$result = $q->fetch();
if($q->rowCount() == 1) {
header('Location: ' . $result['url'], true, 303);
exit();
}
}