Mercurial > hg > egg-tcls
annotate urlredirect.php.txt @ 174:d94be9a1be6a
weather: Add support for listing currently defined aliases.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 03 Jun 2014 13:00:38 +0300 |
parents | 7ed449f9b848 |
children | 68c4b25c982c |
rev | line source |
---|---|
0 | 1 <? |
88
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
2 /* ========================================================== |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
3 * URLLog PHP-script for redirecting ShortURLs |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
4 * (C) Copyright 2006-2011 Tecnic Software productions (TNSP) |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
5 * ========================================================== |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
6 * How to set up short URL redirection: |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
7 * |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
8 * 1) Set up URLLog database (see urllog.tcl for more information) |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
9 * |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
10 * 2) In this example we assume that your short URL prefix is: |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
11 * http://example.com/u/ |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
12 * This setting is called urllog_shorturl_prefix in urllog.tcl |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
13 * |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
14 * 3) Place this PHP script under your www-server root. In this |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
15 * example we will use "/var/www/urlredirect.php" that is |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
16 * visible as http://example.com/urlredirect.php |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
17 * |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
18 * Edit script dbFilename setting below to point to correct |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
19 * URLLog SQLite database file location. |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
20 * |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
21 * 4) Create or edit /var/www/.htaccess and add following: |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
22 * RewriteEngine on |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
23 * RewriteRule ^u/([^\?\&]+)$ /urlredirect.php?u=$1 |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
24 * |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
25 * 5) You may need to restart/reload your www-server. |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
26 * |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
27 * 6) Profit. |
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
28 */ |
17
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
29 // SQLite3 database file path and name |
88
7ed449f9b848
Add documentation for short URL redirector.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
30 $dbFilename = "/path/to/urllog.sqlite"; |
0 | 31 |
51 | 32 // ========================================================== |
0 | 33 // Helper functions |
34 $idStr = "ABCDEFGHIJKLNMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
35 | |
17
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
36 function myerr($str = "") { |
30 | 37 header("Status: 404 Not Found"); |
38 echo "404 Not Found - ".$str; | |
39 exit; | |
0 | 40 } |
41 | |
42 // Check arguments | |
17
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
43 if (isset($_GET["u"])) |
30 | 44 $urlStr = $_GET["u"]; |
17
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
45 else |
30 | 46 myerr("No ID given."); |
0 | 47 |
48 // Calculate urlID | |
49 $urlLen = strlen($urlStr); | |
17
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
50 if ($urlLen < 1) myerr("Invalid ID length."); |
0 | 51 |
52 for ($urlID = 0, $i = 0; $i < $urlLen; $i++) { | |
30 | 53 $urlID *= strlen($idStr); |
54 $n = strpos($idStr, $urlStr[$i]); | |
55 if ($n !== FALSE) { | |
56 $urlID += $n; | |
57 } else | |
58 myerr("Invalid ID."); | |
0 | 59 } |
60 | |
61 // Find the URL | |
17
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
62 try { |
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
63 $db = new PDO("sqlite:".$dbFilename); |
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
64 } |
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
65 catch (PDOException $e) { |
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
66 myerr("Could not connect to URL database: ".$e->getMessage()."."); |
0 | 67 } |
68 | |
17
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
69 foreach ($db->query("SELECT url FROM urls WHERE id=".$urlID) as $row) { |
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
70 header("Location: ".$row["url"]); |
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
71 exit; |
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
72 } |
0 | 73 |
17
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
74 $db = null; |
0 | 75 // Output result |
17
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
76 myerr("No such ID in database."); |
0 | 77 ?> |