/
index.php
44 lines (41 loc) · 921 Bytes
/
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
<?php
// Load config and connect to database
require_once('config.php');
require_once('connect.php');
require_once('functions.php');
// Parse request from uri string
$temp = explode('/', $_SERVER['REQUEST_URI']);
$request = decode($temp[1]);
// Select from database based on decoded ID
$sql = "SELECT id, url, created_at FROM links WHERE id = $request";
$data = mysql_query($sql) or die(mysql_error());
$info = mysql_fetch_assoc( $data );
$num_rows = mysql_num_rows($data);
if ($num_rows < 1)
{
die('ERROR: invalid url');
}
else
{
// Record view
$sql = "
INSERT INTO ".DB_NAME.".`views` (
`view_id` ,
`link_id`,
`user_ip`,
`user_agent`,
`created_at`
)
VALUES (
NULL,
".$info['id'].",
'".$_SERVER['REMOTE_ADDR']."',
'".$_SERVER['HTTP_USER_AGENT']."',
".time()."
)
";
mysql_query($sql) or die(mysql_error());
// Redirect to URL
header('Location: '.$info['url']);
}
?>