Mercurial > hg > egg-tcls
annotate urlredirect.php.txt @ 698:6ba9f961e463 default tip
quotedb: Bump version and copyright.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 18 Sep 2023 11:38:41 +0300 |
parents | 6c999a7ac6d9 |
children |
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 |
307 | 4 * (C) Copyright 2006-2015 Tecnic Software productions (TNSP) |
88
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 |
468
6c999a7ac6d9
Delete trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
307
diff
changeset
|
13 * |
88
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 * |
468
6c999a7ac6d9
Delete trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
307
diff
changeset
|
25 * 5) You may need to restart/reload your www-server. |
88
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 */ |
307 | 29 try { |
30 // You need to uncomment and configure one of the PDO object | |
31 // initialization lines below, depending on what kind of SQL | |
32 // database you are using. | |
33 | |
34 // If using SQLite3, set the filename / path there: | |
35 //$db = new PDO("sqlite:/path/to/urllog.sqlite"); | |
36 } | |
37 catch (PDOException $e) { | |
38 myerr("Could not connect to URL database: ".$e->getMessage()."."); | |
39 } | |
40 | |
0 | 41 |
51 | 42 // ========================================================== |
0 | 43 // Helper functions |
180
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
44 function urlToID($url) |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
45 { |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
46 $idCodes = "ABCDEFGHIJKLNMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
47 $idNumCodes = strlen($idCodes); |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
48 |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
49 $len = strlen($url); |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
50 if ($len < 1) |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
51 return -1; |
0 | 52 |
180
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
53 for ($id = 0, $i = 0; $i < $len; $i++) |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
54 { |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
55 $id *= $idNumCodes; |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
56 $n = strpos($idCodes, $url[$i]); |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
57 if ($n !== FALSE) |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
58 $id += $n; |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
59 else |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
60 return -2; |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
61 } |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
62 return $id; |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
63 } |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
64 |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
65 function myerr($str = "") |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
66 { |
30 | 67 header("Status: 404 Not Found"); |
68 echo "404 Not Found - ".$str; | |
69 exit; | |
0 | 70 } |
71 | |
72 // Check arguments | |
17
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
73 if (isset($_GET["u"])) |
30 | 74 $urlStr = $_GET["u"]; |
17
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
75 else |
30 | 76 myerr("No ID given."); |
0 | 77 |
78 // Calculate urlID | |
180
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
79 $urlID = urlToID($urlStr); |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
80 if ($urlID < 0) |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
81 myerr("Invalid ID."); |
0 | 82 |
83 | |
307 | 84 // Find the matching URL, if any |
180
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
85 foreach ($db->query("SELECT url FROM urls WHERE id=".$urlID) as $row) |
68c4b25c982c
Update the URL redirector PHP script.
Matti Hamalainen <ccr@tnsp.org>
parents:
88
diff
changeset
|
86 { |
17
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
87 header("Location: ".$row["url"]); |
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
88 exit; |
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
89 } |
0 | 90 |
17
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
91 $db = null; |
0 | 92 // Output result |
17
b9c27bad2d9a
Updated version for SQLite3 database.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
93 myerr("No such ID in database."); |
0 | 94 ?> |