Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 56 lines (48 sloc) 1.464 kB
6a70a9c Initial project files
Brian Cray authored
1 <?php
2 /*
3 * First authored by Brian Cray
4 * License: http://creativecommons.org/licenses/by/3.0/
5 * Contact the author at http://briancray.com/
6 */
7
8 ini_set('display_errors', 0);
9
10 if(!preg_match('|^[0-9a-zA-Z]{1,6}$|', $_GET['url']))
11 {
12 die('That is not a valid short url');
13 }
14
15 require('config.php');
16
17 $shortened_id = getIDFromShortenedURL($_GET['url']);
18
19 if(CACHE)
20 {
21 $long_url = file_get_contents(CACHE_DIR . $shortened_id);
22 if(empty($long_url) || !preg_match('|^https?://|', $long_url))
23 {
24 $long_url = mysql_result(mysql_query('SELECT long_url FROM ' . DB_TABLE . ' WHERE id="' . mysql_real_escape_string($shortened_id) . '"'), 0, 0);
25 @mkdir(CACHE_DIR, 0777);
26 $handle = fopen(CACHE_DIR . $shortened_id, 'w+');
27 fwrite($handle, $long_url);
28 fclose($handle);
29 }
30 }
31 else
32 {
33 $long_url = mysql_result(mysql_query('SELECT long_url FROM ' . DB_TABLE . ' WHERE id="' . mysql_real_escape_string($shortened_id) . '"'), 0, 0);
34 }
35
36 if(TRACK)
37 {
38 mysql_query('UPDATE ' . DB_TABLE . ' SET referrals=referrals+1 WHERE id="' . mysql_real_escape_string($shortened_id) . '"');
39 }
40
41 header('HTTP/1.1 301 Moved Permanently');
42 header('Location: ' . $long_url);
43 exit;
44
45 function getIDFromShortenedURL ($string, $base = ALLOWED_CHARS)
46 {
47 $length = strlen($base);
48 $size = strlen($string) - 1;
49 $string = str_split($string);
50 $out = strpos($base, array_pop($string));
51 foreach($string as $i => $char)
52 {
53 $out += strpos($base, $char) * pow($length, $size - $i);
54 }
55 return $out;
56 }
Something went wrong with that request. Please try again.