| Soundcloud 28689 followers |
|
| Mixcloud 2699 followers |
|
| Last.fm 98645 listeners |
|
| Facebook | |
| Discord |
Apr 29th, 2010
You have some files you want to put up for downloads and want to track how many times they have been downloaded ?
Here's an example using jquery, php and mysql. You can download the source files and visit the demo page.
First we need two mysql tables, i've called them db_links and db_links_hits
in index.php import the LinkStat class and connect to the database :
then import all necessary files in your page :
add the class "linkstat" to the links you want to track
when clicking the link, it calls the click function in linkstats.js
the $.post function sends an ajax-post request with the link url as parameter to the php file 'ajax-linkstat.php' that do the job of storing the link hit in the database.
That's all !
Here's an example using jquery, php and mysql. You can download the source files and visit the demo page.
First we need two mysql tables, i've called them db_links and db_links_hits
mysql tables
CREATE TABLE IF NOT EXISTS db_links (
`id` int(11) NOT NULL auto_increment,
`link` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS db_links_hits (
`id` int(11) NOT NULL auto_increment,
`link_id` int(11) NOT NULL,
`date` datetime NOT NULL,
`ip` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
in index.php import the LinkStat class and connect to the database :
require_once "LinkStat.php";
// define connection settings
define('DB_HOST', 'localhost');
define('DB_USER', 'dbuser');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database');
// establish a connection to the database server
if(!$GLOBALS['DB'] = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)) {
die('ERROR : Unable to connect to database server.');
}
if(!mysql_select_db(DB_DATABASE, $GLOBALS['DB'])) {
mysql_close($GLOBALS['DB']);
die('ERROR : Unable to select database schema.');
}
then import all necessary files in your page :
add the class "linkstat" to the links you want to track
click me !
when clicking the link, it calls the click function in linkstats.js
$(document).ready(function(){
$("a.linkstat").click(function(e) {
e.preventDefault(); // needed for safari 4.0.5
$.post("/examples/linkstats/ajax-linkstat.php", {url: $(this).attr('href')}, function success(data) {
window.location = data; // needed for safari 4.0.5
});
return true;
});
});
the $.post function sends an ajax-post request with the link url as parameter to the php file 'ajax-linkstat.php' that do the job of storing the link hit in the database.
require_once "LinkStat.php";
// ... connect to database ...
// insert new link hit in the database
LinkStat::addHit($_POST['url'], getIp());
// return url for redirect
echo $_POST['url'];
// function to get the user ip address
function getIp() {
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
}
elseif (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_X_FORWARDED')) {
$ip = getenv('HTTP_X_FORWARDED');
}
elseif (getenv('HTTP_FORWARDED_FOR')) {
$ip = getenv('HTTP_FORWARDED_FOR');
}
elseif (getenv('HTTP_FORWARDED')) {
$ip = getenv('HTTP_FORWARDED');
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
That's all !




