Full On Design

Berkshire Based Web Development

 

Coding

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.

IE8 now a high priority update

I said earlier this week that IE8 will soon be a high priority update; well today I noticed that IE8 has now reach high priority update status.

Essentially this means that IE8 is now available over windows automatic update and should slowly (over the next few weeks) start replacing most IE6 and IE7 installs.

Decrease loading times via .htaccess

Here is a really nifty trick I’ve been using for a while to decrease the time a page takes to load. Add the following lines of code to your .htaccess file:

FileETag none # Turn off eTags
<IfModule mod_expires.c> # Check that the expires module has been installed
ExpiresActive On
ExpiresDefault "access plus 10 years"
ExpiresByType image/gif "access plus 10 years"
ExpiresByType image/jpeg "access plus 10 years"
ExpiresByType image/png "access plus 10 years"
ExpiresByType text/css "access plus 10 years"
ExpiresByType text/html "access plus 1 seconds"
ExpiresByType text/javascript "access plus 10 years"
ExpiresByType application/x-unknown-content-type "access plus 10 years"
ExpiresByType application/x-javascript "access plus 10 years"
</IfModule>
<IfModule mod_gzip.c>  # check if gZip support has been installed
mod_gzip_on         Yes
mod_gzip_dechunk    Yes
mod_gzip_item_include file          \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler       ^cgi-script$
mod_gzip_item_include mime      ^text\.*
mod_gzip_item_include mime      ^application/x-javascript.*
mod_gzip_item_exclude mime      ^image\.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>

This quick and easy method tells the user to cache files which are unlikely to change for 10 years (feel free to change the amount of time) and HTML for 1 second. It also turns off eTags.

Update: I also added a another piece of code I use which turns on Gzip, which reduces the amount of bandwidth required to transfer a file.

Internet Statistics – March 2009

Browser Statistics

march-09_browser-trendIE8 has been released, but unfortunately will not be a high priority until around the 20th April 2009.

Biggest Rise: Safari (3.09%)
Biggest Fall: Other (-2.82%)

OS Statistics

march-09_os-averageWinXP is still very dominant, but is losing ground to Vista and other OS’s.

Biggest Rise: Vista (0.31%)
Biggest Fall: WinXP (-0.39%)

Internet Penetration

march-09_internet-penetration-trend

Latin America/Caribbean and North America had the same increase in Internet Penetration in March 2009. Could the gradual increase in Internet Penetration be a sign that the recession is soon to subside?

Internet Usage

march-09_internet-usage-trend

Internet Usage stayed relatively constant in relation the previous month, even though there was an increase in internet penetration.

Conditional Comments

Most coders and designers will tell you Internet Explorer (Especially IE6 below) is a pain to work with, but I tend to disagree. What most people forget is that even though IE is full of bugs and oddities everything can be fixed, with just a few lines of code.

How to use Conditional Comments

Conditional Comments let you execute sections of code in just IE, so you can import custom CSS sheets (to deal with IE’s weird rendering engine) or special JavaScript; here is how to use conditional comments:

<!--[if IE]>
This will only show for Internet Explorer (IE)
<![endif]-->
<!--[if !IE]>
This will only show if the client is not using IE.
<!--[endif]>
<![if IE 6]-->
Only shows on IE6
<!--[endif]>
<![if lte IE 6]-->
Any IE browser less than or equal to version 6.
<!--[endif]>
<![if gte IE 7]-->
Any IE browser higher or equal to IE 7
<!--[endif]>

Just place snippets of the above code anywhere in your code. If you want more information conditional commenting take a look at the Conditional Comment Wikipedia page.