New sites in the FollowMe widget
I've just added Soundcloud and Mixcloud in the FollowMe widget.
I also simplified a bit the design so it integrates better in any sites .

Check it out here.

Link statistics with jquery, php and mysql
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

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 !



SyntaxHighlighter
i was searching for a way to display code in my blog if i needed to and i found this javascript syntax highlighter by Alex Gorbatchev that did the job really well !

just include the javascript files and css styles :









then enclose your content inside pre tags with a brush class attribute :



link




as i was already converting \n characters to html br tags they were also showing in the code output. i just had to edit shCore.js and change the value of stripBrs from false to true :


stripBrs : false,


to


stripBrs : true,