Skip to content
Browse files

Apply @beverloo’s advice as much as possible.

Also, some other minor tweaks:

* Switch to tabs for indentation.
* Some code style fixes (personal preference).
* Add Peter as a contributor.
  • Loading branch information...
1 parent b5eb1b3 commit 92c616d274b99240478510109862902b3d83fa75 @mathiasbynens committed Jun 21, 2011
Showing with 81 additions and 76 deletions.
  1. +13 −3 README.md
  2. +5 −5 database.sql
  3. +38 −43 index.php
  4. +25 −25 shorten.php
View
16 README.md
@@ -22,10 +22,20 @@
### Prompt
- javascript:(function(){var%20q=prompt('URL:');if(q){document.location='http://yourshortener.ext/shorten?url='+encodeURIComponent(q)}})();
+``` js
+javascript:(function(){var%20q=prompt('URL:');if(q){document.location='http://yourshortener.ext/shorten?url='+encodeURIComponent(q)}})();
+```
### Shorten this URL
- javascript:(function(){document.location='http://yourshortener.ext/shorten?url='+encodeURIComponent(location.href)})();
+``` js
+javascript:(function(){document.location='http://yourshortener.ext/shorten?url='+encodeURIComponent(location.href)})();
+````
-_— [Mathias](http://mathiasbynens.be/)_
+## Author
+
+* [Mathias Bynens](http://mathiasbynens.be/)
+
+## Contributors
+
+* [Peter Beverloo](http://peter.sh/)
View
10 database.sql
@@ -1,9 +1,9 @@
CREATE TABLE `redirect` (
- `slug` varchar(14) collate utf8_unicode_ci NOT NULL,
- `url` varchar(620) collate utf8_unicode_ci NOT NULL,
- `date` datetime NOT NULL,
- `hits` bigint(20) NOT NULL default '0',
- PRIMARY KEY (`slug`)
+ `slug` varchar(14) collate utf8_unicode_ci NOT NULL,
+ `url` varchar(620) collate utf8_unicode_ci NOT NULL,
+ `date` datetime NOT NULL,
+ `hits` bigint(20) NOT NULL default '0',
+ PRIMARY KEY (`slug`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Used for the URL shortener';
INSERT INTO `redirect` VALUES ('a', 'https://github.com/mathiasbynens/php-url-shortener', NOW(), 1);
View
81 index.php
@@ -1,50 +1,45 @@
<?php
+
require 'config.php';
$redirectDestination = DEFAULT_URL . '/';
-if (isset ($_GET ['slug']))
-{
- $slug = preg_replace ('/[^a-z0-9]/si', '', $_GET ['slug']);
- if (is_numeric ($slug) && strlen ($slug) > 8)
- {
- $redirectDestination = 'http://twitter.com/' . TWITTER_USERNAME . '/status/' . $slug;
- }
- else
- {
- $database = new MySQLi (MYSQLI_HOST, MYSQLI_USER, MYSQLI_PASSWORD, MYSQLI_DATABASE);
- $database -> set_charset ('utf8');
-
- $escapedSlug = $database -> real_escape_string ($slug);
- $redirectResult = $database -> query ('SELECT url FROM redirect WHERE slug="' . $escapedSlug . '"');
-
- if ($redirectResult !== false && $redirectResult -> num_rows != 0)
- {
- $database -> query ('UPDATE redirect SET hits=hits+1 WHERE slug="' . $escapedSlug . '"');
- $redirectDestination = $redirectResult -> fetch_object () -> url;
- }
- else
- {
- $redirectDestination = DEFAULT_URL . $_SERVER ['REQUEST_URI'];
- }
-
- $database -> close ();
- }
+
+if (isset($_GET['slug'])) {
+
+ $slug = preg_replace('/[^a-z0-9]/si', '', $_GET['slug']);
+ if (is_numeric ($slug) && strlen ($slug) > 8) {
+ $redirectDestination = 'http://twitter.com/' . TWITTER_USERNAME . '/status/' . $slug;
+
+ } else {
+
+ $db = new MySQLi(MYSQLI_HOST, MYSQLI_USER, MYSQLI_PASSWORD, MYSQLI_DATABASE);
+ $db->set_charset('utf8');
+
+ $escapedSlug = $db->real_escape_string($slug);
+ $redirectResult = $db->query('SELECT url FROM redirect WHERE slug = "' . $escapedSlug . '"');
+
+ if ($redirectResult !== false && $redirectResult->num_rows != 0) {
+ $db->query ('UPDATE redirect SET hits = hits + 1 WHERE slug = "' . $escapedSlug . '"');
+ $redirectDestination = $redirectResult->fetch_object()->url;
+ } else {
+ $redirectDestination = DEFAULT_URL . $_SERVER['REQUEST_URI'];
+ }
+
+ $db->close();
+
+ }
}
-Header ('Location: ' . $redirectDestination, null, 301);
+header('Location: ' . $redirectDestination, null, 301);
+
?>
-<!doctype html>
-<html lang="en">
- <head>
- <title>Redirecting…</title>
- <meta http-equiv="refresh" content="0; URL=<?php echo $redirectDestination; ?>" />
- <script>
- try {
- document.location.href = "<?php echo $redirectDestination; ?>";
- } catch (ex) {}
- </script>
- </head>
- <body>
- <a href="<?php echo $redirectDestination; ?>">Click here to continue…</a>
- </body>
-</html>
+<!DOCTYPE html>
+<meta charset=utf-8>
+<title>Redirecting…</title>
+<meta http-equiv=refresh content="0; URL=<?php echo $redirectDestination; ?>">
+<a href="<?php echo $redirectDestination; ?>">Click here to continue to <?php echo $redirectDestination; ?>…</a>
+<script>
+ try {
+ document.location.href = '<?php echo $redirectDestination; ?>';
+ } catch (e) {}
+</script>
View
50 shorten.php
@@ -1,50 +1,50 @@
<?php
-require('config.php');
+require 'config.php';
header('Content-Type: text/plain;charset=UTF-8');
$url = isset($_GET['url']) ? urldecode(trim($_GET['url'])) : '';
if (in_array($url, array('', 'about:blank', 'undefined', 'http://localhost/'))) {
- die('Enter a URL.');
+ die('Enter a URL.');
}
function nextLetter(&$str) {
- $str = ('z' === $str ? 'a' : ++$str);
+ $str = ('z' === $str ? 'a' : ++$str);
}
function getNextShortURL($s) {
- $a = str_split($s);
- $c = count($a);
- if (preg_match('/^z*$/', $s)) { // string consists entirely of `z`
- return str_repeat('a', $c + 1);
- }
- while ('z' === $a[--$c]) {
- nextLetter($a[$c]);
- }
- nextLetter($a[$c]);
- return implode($a);
+ $a = str_split($s);
+ $c = count($a);
+ if (preg_match('/^z*$/', $s)) { // string consists entirely of `z`
+ return str_repeat('a', $c + 1);
+ }
+ while ('z' === $a[--$c]) {
+ nextLetter($a[$c]);
+ }
+ nextLetter($a[$c]);
+ return implode($a);
}
$db = new mysqli(MYSQLI_HOST, MYSQLI_USER, MYSQLI_PASSWORD, MYSQLI_DATABASE);
-$db->query('SET NAMES "utf8"');
+$db->set_charset('utf8');
$url = $db->real_escape_string($url);
-$result = $db->query('SELECT `slug` FROM `redirect` WHERE `url` = "' . $url . '" LIMIT 1');
+$result = $db->query('SELECT slug FROM redirect WHERE url = "' . $url . '" LIMIT 1');
if ($result && $result->num_rows > 0) { // If there’s already a short URL for this URL
- die(SHORT_URL . $result->fetch_object()->slug);
+ die(SHORT_URL . $result->fetch_object()->slug);
} else {
- $result = $db->query('SELECT `slug`, `url` FROM `redirect` ORDER BY `date` DESC LIMIT 1');
- if ($result && $result->num_rows > 0) {
- $slug = getNextShortURL($result->fetch_object()->slug);
- if ($db->query('INSERT INTO `redirect` (`slug`, `url`, `date`, `hits`) VALUES ("' . $slug . '", "' . $url . '", NOW(), 0)')) {
- header('HTTP/1.1 201 Created');
- echo SHORT_URL . $slug;
- $db->query('OPTIMIZE TABLE `redirect`');
- }
- }
+ $result = $db->query('SELECT slug, url FROM redirect ORDER BY date DESC LIMIT 1');
+ if ($result && $result->num_rows > 0) {
+ $slug = getNextShortURL($result->fetch_object()->slug);
+ if ($db->query('INSERT INTO redirect (slug, url, date, hits) VALUES ("' . $slug . '", "' . $url . '", NOW(), 0)')) {
+ header('HTTP/1.1 201 Created');
+ echo SHORT_URL . $slug;
+ $db->query('OPTIMIZE TABLE `redirect`');
+ }
+ }
}
?>

0 comments on commit 92c616d

Please sign in to comment.
Something went wrong with that request. Please try again.