diff --git a/README b/README new file mode 100644 index 0000000..45ee4c9 --- /dev/null +++ b/README @@ -0,0 +1,36 @@ +License: http://creativecommons.org/licenses/by/3.0/ + +Benefits + +- Can shorten over 42 billion unique URLs in 6 or less characters (it can do more than 12,000,000 in only 4!) +- Super duper fast—uses very little server resources +- Includes an API so you can create your own short URLs on the fly +- Option to turn clickthru tracking on and off +- Option to limit usage to 1 IP address for personal use and to prevent spamming from others +- Only uses alphanumeric characters so all browsers can interpret the URL +- Secure—several data filters in place to prevent SQL injection hacks +- Option to check if the URL is real (doesn’t respond with a 404) before shortening +- Uses 301 redirects for SEO and analytics yumminess +- Option to store a local cache to prevent database queries on every redirect +- Option to change the characters allowed in a shortened url + +Installation + +1. Make sure your server meets the requirements: + a) Optionally you can run this from your current domain or find a short domain + b) Apache + c) PHP + d) MySQL + e) Access to run SQL queries for installation +2. Download a .zip file of the PHP URL shortener script files +3. Upload the contents of the .zip file to your web server +4. Update the database info in config.php +5. Run the SQL included in shortenedurls.sql. Many people use phpMyAdmin for this, if you can’t do it yourself contact your host. +6. Rename rename.htaccess to .htaccess +7. If you want to use the caching option, create a directory named cache with permissions 777 + +Using your personal URL shortener service + +- To manually shorten URLs open in your web browser the location where you uploaded the files. +- To programmatically shorten URLs with PHP use the following code: + $shortenedurl = file_get_contents('http://yourdomain.com/shorten.php?longurl=' . urlencode('http://' . $_SERVER['HTTP_HOST'] . '/' . $_SERVER['REQUEST_URI'])); diff --git a/config.php b/config.php new file mode 100644 index 0000000..8e8aaef --- /dev/null +++ b/config.php @@ -0,0 +1,38 @@ + +/* + * First authored by Brian Cray + * License: http://creativecommons.org/licenses/by/3.0/ + * Contact the author at http://briancray.com/ + */ + +// db options +define('DB_NAME', 'your db name'); +define('DB_USER', 'your db usernae'); +define('DB_PASSWORD', 'your db password'); +define('DB_HOST', 'localhost'); +define('DB_TABLE', 'shortenedurls'); + +// connect to database +mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); +mysql_select_db(DB_NAME); + +// base location of script (include trailing slash) +define('BASE_HREF', 'http://' . $_SERVER['HTTP_HOST'] . '/'); + +// change to limit short url creation to a single IP +define('LIMIT_TO_IP', $_SERVER['REMOTE_ADDR']); + +// change to TRUE to start tracking referrals +define('TRACK', FALSE); + +// check if URL exists first +define('CHECK_URL', FALSE); + +// change the shortened URL allowed characters +define('ALLOWED_CHARS', '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); + +// do you want to cache? +define('CACHE', TRUE); + +// if so, where will the cache files be stored? (include trailing slash) +define('CACHE_DIR', dirname(__FILE__) . '/cache/'); \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..7d619f9 --- /dev/null +++ b/index.html @@ -0,0 +1,21 @@ +

If you are seeing this, your .htaccess file is configured incorrectly or doesn't exist.

+

Copy and paste the code below into your .htaccess file

+
+DirectoryIndex index.php
+
+php_flag register_globals off
+php_flag magic_quotes_gpc off
+
+FileETag none
+ServerSignature Off
+
+Options All -Indexes
+
+<IfModule mod_rewrite.c>
+RewriteEngine On
+RewriteRule ^shorten/(.*)$ shorten.php?longurl=$1 [L]
+RewriteRule ^([0-9a-zA-Z]{1,6})$ redirect.php?url=$1 [L]
+</IfModule>
+
+ +

For detailed instructions, visit Brian Cray

\ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..9bdd482 --- /dev/null +++ b/index.php @@ -0,0 +1,23 @@ + + +URL shortener + + + +
+ +
+ + + + + \ No newline at end of file diff --git a/redirect.php b/redirect.php new file mode 100644 index 0000000..d929209 --- /dev/null +++ b/redirect.php @@ -0,0 +1,56 @@ + $char) + { + $out += strpos($base, $char) * pow($length, $size - $i); + } + return $out; +} \ No newline at end of file diff --git a/rename.htaccess b/rename.htaccess new file mode 100644 index 0000000..347695d --- /dev/null +++ b/rename.htaccess @@ -0,0 +1,17 @@ +DirectoryIndex index.php + +# remove the next 3 lines if you see a 500 server error +php_flag register_globals off +php_flag magic_quotes_gpc off +php_value display_errors 0 + +FileETag none +ServerSignature Off + +Options All -Indexes + + +RewriteEngine On +RewriteRule ^shorten/(.*)$ shorten.php?longurl=$1 [L] +RewriteRule ^([0-9a-zA-Z]{1,6})$ redirect.php?url=$1 [L] + \ No newline at end of file diff --git a/shorten.php b/shorten.php new file mode 100644 index 0000000..cfe0f00 --- /dev/null +++ b/shorten.php @@ -0,0 +1,63 @@ + $length - 1) + { + $out = $base[fmod($integer, $length)] . $out; + $integer = floor( $integer / $length ); + } + return $base[$integer] . $out; +} \ No newline at end of file diff --git a/shortenedurls.sql b/shortenedurls.sql new file mode 100644 index 0000000..fc01bb6 --- /dev/null +++ b/shortenedurls.sql @@ -0,0 +1,14 @@ +-- +-- Table structure for table `shortenedurls` +-- + +CREATE TABLE `shortenedurls` ( + `id` int(10) unsigned NOT NULL auto_increment, + `long_url` varchar(255) NOT NULL, + `created` int(10) unsigned NOT NULL, + `creator` char(15) NOT NULL, + `referrals` int(10) unsigned NOT NULL default '0', + PRIMARY KEY (`id`), + UNIQUE KEY `long` (`long_url`), + KEY `referrals` (`referrals`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; \ No newline at end of file