Skip Navagation

Full On Design

A Web Development & Technology Blog

 

Simple PHP Download Counter

Here is a really easy and lightweight way of logging the amount of downloads.

<?php # File created on 25th April 2009 by Mike Rogers (http://www.fullondesign.co.uk/). 

## Start defining constants ##
define(LOG_URL, '/home/user/download_logs/'); // Put the location of where you want to put the logs. Make sure this is absolute
# $_GET['ID'] - this is the ID of the file we want. It gets the value from the URL.

## Now set the data you wish to use - this can be moved to an include if you want ##
$file[0] = 'http://www.example.com/file.pdf';

## Define the functions required to update the file.
function update_file($filename, $value){
	if(is_writable($filename) && is_readable($filename)){
		if(file_put_contents($filename, $value)){
			return TRUE;
		}
	}
	return NULL;
}
function pull_file($filename){
	if(is_writable($filename) && is_readable($filename)){
		return file_get_contents($filename);
	}
	return NULL;
}
function rebuild_file($filename){
	if(is_writable($filename) && is_readable($filename)){
		file_put_contents($filename, '1');
	}
}

if(is_numeric($_GET['ID'])){ // Meaning the Data sent is safe.

	// Build log file URL
	$filename = LOG_URL.$_GET['ID'].'.log.txt';
	$value = pull_file($filename);

	header('location:'.$file[$_GET['ID']]);

	// Update logs
	if(is_numeric($value)){
		update_file($filename, ($value+1));
	} else { // Meaning the file does not exist or has been messed with.
		rebuild_file($filename);
	}
} else {
	echo 'Sorry, there was an error.';
}

/* If you want to see how many people have downloaded a file, run something like:
# pull_file(LOG_URL.numberishere.'.log.txt');
/*

You are free to share, modify and use this code for commercial uses. Please give a link back (to http://www.fullondesign.co.uk/ ) if you can, but you don't have you.

I claim no liability for this code, you use it at your own risk.

*/

?>

You can run this by changing the URL that accesses the file. For example:
file.php?ID=0

Feel free to edit and share this code.

7 Harsh truths about your forums

I love forums, they are a great way to communicate but I find that very often I get to a website’s forums and find it is about as interesting as paint drying. Below are some of the complaints I tend to have for the webmasters.

1. Your users are idiots

This is a common problem on support forums, where users will post the exactly same problem…several times (Sometimes you may even find it’s the same user). This is unbelievably infuriating because it shows the person who wants help, can’t be bothered to even do a quick search.

The best solution is to encourage user to check before they post (such as a simple search of previous posts, like on Digg). This will reduce the number of very similar posts.

2. You have too much choice

Keep your forum relevant to your website; I don’t want to go to a Surfing website and end up talking about Web Design. Try and limit yourself to around 2 to 4 forums with a maximum of 4 sub-forums. Otherwise you are overloading your users with choice.

3. You have no community

No one likes the feeling that they are talking to themselves, especially on forums. Make sure you (and everyone you know) use your forums, to make it feel more active.

Having an easily accessible “Recent posts” or “Active Topics” page will instantaneously give smaller forums a feeling of community.

Read the rest of this entry »