Skip to content

Commit

Permalink
Alphanumeric shorturls
Browse files Browse the repository at this point in the history
+ Now we do not use id=123 type shorturls, instead we use
  4 character long alpha-numeric shorturls, for example 1aG6
  • Loading branch information
stargazers committed Nov 3, 2010
1 parent 7c7814d commit 942ac01
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .htaccess
@@ -1,3 +1,3 @@
RewriteEngine On
RewriteBase /shorturl
RewriteRule ^([a-z0-9]+)$ index.php?id=$1
RewriteRule ^([A-Za-z0-9]+)$ index.php?id=$1
43 changes: 43 additions & 0 deletions CHTML/CHTML.php
Expand Up @@ -29,6 +29,49 @@
// **************************************************
class CHTML
{
// **************************************************
// createRandomString
/*!
@brief Generate random alphanumeric string.
This can be used in password generations,
shorturls and so on.
@param $len Length of string to generate
@return Random alpahnumeric string.
*/
// **************************************************
function createRandomString( $len )
{
// Characters what can be in random string
$ok_val = array();

// Add number 0-9
for( $i=0; $i<10; $i++ )
$ok_val[] = $i;

// Add characters A to Z
for( $i=65; $i<91; $i++ )
$ok_val[] = chr( $i );

// Add characters a to z
for( $i=97; $i<123; $i++ )
$ok_val[] = chr( $i );

$text = '';

// Count size of array $ok_val before
// for-loop, because we do not want to
// count it every time because it cannot
// change on the fly. Optimizations ftw! ;)
$max = count( $ok_val );

// Create correct lenght random string
for( $c=0; $c<$len; $c++ )
$text .= $ok_val[mt_rand( 0, $max )];

return $text;
}

// **************************************************
// makeSafeForDB
Expand Down
59 changes: 43 additions & 16 deletions index.php
Expand Up @@ -103,7 +103,7 @@ function create_site_footer( $html )
@param $html CHTML class instance.
@param $id ID in database where we redirect user.
@param $id shorturl in database where we redirect user.
@return None.
*/
Expand All @@ -115,7 +115,7 @@ function redirect_to( $db, $html, $id )
$url = 'index.php';

$id = $html->makeSafeForDB( $id );
$q = 'SELECT url FROM shorturl WHERE id=' . $id;
$q = 'SELECT url FROM shorturl WHERE shorturl="' . $id . '"';

try
{
Expand Down Expand Up @@ -143,23 +143,23 @@ function redirect_to( $db, $html, $id )
@param $db CSQLite database class instance.
@param $id INSERT id for this shortURL.
@param $su Generated ShortURL.
@param $html CHTML class instance.
@return None.
*/
// **************************************************
function show_given_shorturl( $db, $id, $html )
function show_given_shorturl( $db, $su, $html )
{
$html->createSiteTop( 'ShortURL', 'shorturl.css' );

echo '<div id="top_logo">';
echo 'ShortURL';
echo '</div>';
echo '<div id="given_url">';
echo 'Generated URL is <a href="http://s.runosydan.net/?id='
. $id . '">http://s.runosydan.net/?id=' . $id . '</a>';
echo 'Generated URL is <a href="http://s.runosydan.net/'
. $su . '">http://s.runosydan.net/' . $su . '</a>';
echo '<br /><br />';
echo '<a href="index.php">Back to mainpage</a>';
echo '</div>';
Expand Down Expand Up @@ -204,7 +204,7 @@ function open_connection()
if( $create_db )
{
$q = 'CREATE TABLE shorturl ( id INTEGER PRIMARY KEY, '
. 'url TEXT, added DATETIME );';
. 'url TEXT, added DATETIME, shorturl TEXT );';

$db->query( $q );
}
Expand Down Expand Up @@ -256,10 +256,10 @@ function process_post_data( $db, $html )
$url = 'http://' . $url;

// Add validated URL to database.
$last_id = add_to_database( $db, $url );
$shorturl = add_to_database( $db, $url, $html );

// Show given shortURL to user too.
show_given_shorturl( $db, $last_id, $html );
show_given_shorturl( $db, $shorturl, $html );
}

// **************************************************
Expand All @@ -272,27 +272,54 @@ function process_post_data( $db, $html )
@param $url URL. Note! URL must be validated before
you call this or the whole universe might explode!
@param $html CHTML class instance.
@return ID of last insert.
*/
// **************************************************
function add_to_database( $db, $url )
function add_to_database( $db, $url, $html )
{
// Create database query in variable
$q = 'INSERT INTO shorturl VALUES( '
. 'NULL, '
. '"' . $url . '",'
. '"' . date( 'Y-m-d H:i:s' ) . '" )';
$random_string = '';

// Generate random string what does not exists
// already in our database.
while( true )
{
$random_string = $html->createRandomString( 4 );

$q = 'SELECT id FROM shorturl WHERE shorturl="'
. $random_string . '"';

try
{
$ret = $db->query( $q );
if( $db->numRows( $ret ) == 0 )
break;
}
catch( Exception $e )
{
die( 'Error in query! ' . $e->getMessage() );
}
}

try
{
// Create database query in variable
$q = 'INSERT INTO shorturl VALUES( '
. 'NULL, '
. '"' . $url . '",'
. '"' . date( 'Y-m-d H:i:s' ) . '",'
. '"' . $random_string . '" )';

$db->query( $q );

}
catch( Exception $e )
{
die( 'Failed! Error was ' . $e->getMessage() );
}

return $db->getLastInsertID();
return $random_string;
}

// **************************************************
Expand Down

0 comments on commit 942ac01

Please sign in to comment.