Skip to content

Commit

Permalink
Simple statistics to database, no UI yet.
Browse files Browse the repository at this point in the history
+ Now we save information to database when someone have
  used our shortURL. We save used shorturl and datetime when
  that shorturl was used.
  • Loading branch information
stargazers committed Nov 3, 2010
1 parent 942ac01 commit 566e1fb
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions index.php
Expand Up @@ -92,6 +92,35 @@ function create_site_footer( $html )
echo '</div>';
}

// **************************************************
// add_to_stats
/*!
@brief Add information about URL usage to stats table.
@param $db CSQLite database class instance.
@param $url Shortened URL where we are going.
@return None.
*/
// **************************************************
function add_to_stats( $db, $url )
{
$q = 'INSERT INTO stats VALUES( NULL, '
. '"' . $url . '",'
. '"' . date( 'Y-m-d H:i:s' ) . '" )';

try
{
$db->query( $q );
}
catch( Exception $e )
{
echo 'Error in stats query! ' . $e->getMessage();
die();
}
}

// **************************************************
// redirect_to
/*!
Expand Down Expand Up @@ -132,6 +161,11 @@ function redirect_to( $db, $html, $id )
die( 'Failed! Error was: ' . $e->getMessage() );
}

// If we are going on the other URL than our mainpage,
// then we want to collect some stats about URL usage.
if( $url != 'index.php' )
add_to_stats( $db, $id );

header( 'Location: ' . $url );
}

Expand Down Expand Up @@ -203,10 +237,17 @@ function open_connection()
// also a table for it.
if( $create_db )
{
// Table for shorturls
$q = 'CREATE TABLE shorturl ( id INTEGER PRIMARY KEY, '
. 'url TEXT, added DATETIME, shorturl TEXT );';

$db->query( $q );

// Table for stats
$q = 'CREATE TABLE stats ( id INTEGER PRIMARY KEY, '
. 'shorturl TEXT, visited DATETIME );';

$db->query( $q );
}
}
catch( Exception $e )
Expand Down

0 comments on commit 566e1fb

Please sign in to comment.