Created
October 7, 2012 13:47
-
-
Save ronaldsgailis/3848442 to your computer and use it in GitHub Desktop.
Files for simple url shortener in PHP and MySQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RewriteEngine On | |
RewriteBase / | |
RewriteRule ^shorten/(.*)$ shorten.php?url=$1 [L,QSA] | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteRule ^(.*)$ index.php?code=$1 [L,QSA] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$config = array(); | |
//MySQL config values | |
$config['mysql_host'] = 'localhost'; | |
$config['mysql_user'] = 'shortener'; | |
$config['mysql_password'] = 'shortener password'; | |
$config['mysql_db'] = 'shortener'; | |
//website to use app on | |
$config['domain'] = 'http://shorte.en/'; | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE IF NOT EXISTS `shortener` ( | |
`shortcode` char(5) COLLATE utf8_unicode_ci NOT NULL, | |
`real_url` text(255) COLLATE utf8_unicode_ci NOT NULL, | |
`date_added` datetime NOT NULL, | |
`visits` bigint(20) NOT NULL DEFAULT '0', | |
PRIMARY KEY (`shortcode`) | |
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* create short url either by generating short code or splitting it up and using the last part as short url | |
* | |
* @param string $url long url to be shortened | |
* @param boolean $compact if we should compact last part to 5 characters | |
* @return string | |
*/ | |
function getShortURL($url, $compact = true) { | |
//use urls last part as is for short code | |
if(!$compact) { | |
$url_parts = parse_url($url); | |
$short_url = str_replace('www.', '', $url_parts['host']).'-'; | |
$parts = array_reverse(explode('/', $url_parts['path'])); | |
$short_url .= preg_replace('/[^a-z0-9_-\.]/i', '', str_replace('-', '_', $parts[0])); | |
} else { | |
$short_url = substr(hash('sha512', time()), 0, 5); | |
} | |
$i = 0; | |
$original_short_url = $short_url; | |
while(isOccupied($short_url)) { | |
if($i > 0) { | |
if($compact) { | |
$short_url = substr(hash('sha512', time()), 0, 5); | |
} else { | |
$short_url = $original_short_url.'_'.$i; | |
} | |
} | |
$i++; | |
} | |
return $short_url; | |
} | |
/** | |
* returns true if selected short url is occupied | |
* | |
* @param string $str | |
* @return boolean | |
*/ | |
function isOccupied($str) { | |
global $db; | |
$query = "SELECT COUNT(*) FROM shortener WHERE shortcode = '{$str}'"; | |
$result = $db->query($query); | |
$rows = $result->fetch_array(); | |
return $rows[0] === 1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'config.php'; | |
if (isset($_GET['code'])) { | |
$code = strip_tags(strtolower($_GET['code'])); | |
$db = new MySQLi($config['mysql_host'], $config['mysql_user'], $config['mysql_password'], $config['mysql_db']); | |
$db->set_charset('utf8'); | |
$escapedCode = $db->real_escape_string($code); | |
$redirectResult = $db->query('SELECT real_url FROM shortener WHERE shortcode = "' . $escapedCode . '"'); | |
if ($redirectResult && $redirectResult->num_rows > 0) { | |
$db->query('UPDATE shortener SET visits = visits + 1 WHERE shortcode = "' . $escapedCode . '"'); | |
$url = $redirectResult->fetch_object()->real_url; | |
} else { | |
die('No such url found!'); | |
} | |
$db->close(); | |
header('Location: ' . $url, null, 301); | |
exit; | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'config.php'; | |
require 'functions.php'; | |
header('Content-Type: text/plain;charset=UTF-8'); | |
$url = isset($_GET['url']) ? urldecode(trim($_GET['url'])) : ''; | |
if (!filter_var($url, FILTER_VALIDATE_URL)) { | |
die('Enter a URL.'); | |
} | |
$db = new mysqli($config['mysql_host'], $config['mysql_user'], $config['mysql_password'], $config['mysql_db']); | |
$db->set_charset('utf8'); | |
$url = $db->real_escape_string($url); | |
$result = $db->query('SELECT shortcode FROM shortener WHERE real_url = "' . $url . '" LIMIT 1'); | |
// If there’s already a short URL for this URL | |
if ($result && $result->num_rows > 0) { | |
die($config['website'] . $result->fetch_object()->shortcode); | |
} else { | |
$shortcode = getShortURL($url); | |
if ($db->query('INSERT INTO shortener (shortcode, real_url, date_added, visits) VALUES ("' . $shortcode . '", "' . $url . '", NOW(), 0)')) { | |
header('HTTP/1.1 201 Created'); | |
echo $config['website'] . $shortcode; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment