how to write a download counter of your own in PHP

The following PHP script does the job of tracking your downloads – like who (IP) downloaded what. It does the following operations
  1. accept the file download request in a particular format – for ex : www.xyz.com/download.php?src=filename.ext
  2. verifies the presence of the file
  3. serves the file for download
  4. log the details – client’s IP and the file downloaded – into a file
<?php
//set 404 page Not Found page
$errorpage=”Location: http://www.mysite.com/404.htm”;

//name of the file to be downloaded
$filename=$_GET["src"];

//checking for file name being empty
if (empty($filename))
{
header($errorpage); //404 page
exit();
}

//checking for file existance
if (!file_exists($filename))
{
header($errorpage); //404 page
exit();
}

//prep the requested file for download

//send header
$mtype=”application/force-download”;
$mtype=$_FILES[$filename]['type'];
$fsize=filesize($filename);
header(“Pragma: public”);
header(“Expires: 0″);
header(“Cache-Control: must-revalidate, post-check=0, pre-check=0″);
header(“Cache-Control: public”);
header(“Content-Description: File Transfer”);
header(“Content-Type: $mtype”);
header(“Content-Disposition: attachment; filename=\”$filename\”");
header(“Content-Transfer-Encoding: binary”);
header(“Content-Length: ” . $fsize);

//start sending out the file contents
$fileptr=@fopen($filename,”rb”);
if ($fileptr) {
while (!feof($fileptr)) {
print(fread($fileptr,1024*8));
flush();
if (connection_status()!=0) {
@fclose($fileptr);
die();
}
}
@fclose($fileptr);
}
else {
header($errorpage); //404page
die();
}

//LOGGING successful download in to a file

//file is opened in append mode
$handle=fopen(“download.log”,”a+”);

//IP from which the request is issued is available from $_SERVER macro
$remoteipaddress=$_SERVER["REMOTE_ADDR"];

//values are written in to the file for future reference
if ($handle != FALSE) {
//log who downloaded what
fwrite ($handle, $remoteipaddress.” – “.$filename.”\n”);
fflush ($handle);
}
fclose($handle);
?>
The data available from this can be used in a number of ways – like understanding the download pattern over a period of time, demand from around the world, …
note :
You can use the following code snippet to serve the file [source]
Use this after the comment “//start sending out the file contents” line :
ob_clean();
flush();
readfile($file);

how to enable confirm exit on chrome

The biggest feature lacking in chrome (as of today 03 February 2010) is “Confirm on Exit” prompt. Countless times I have cursed chrome when I accidentally end up closing chrome rather than a tab.

Solution / Relief

Confirm Close Chrome Plugin

A life saver from Pauvan (blog)

Download link

Cheers man! :-)

Imp Update : The plugin seems to be a little shaky – Its not working in my 4.0.249.78 version of Chrome (Windows)

how to get CPU load as a percentage

Googling is an addiction – but it is distressing if you can’t find a result with the first 10 minutes of search. I searched for an hour to locate this guy.

Requirement : To plot/record CPU load summary as a percentage values as is reported by Task Manager (in windows) or System Monitor  (in Linux)

Solution : Use “sar

Usage :

$ sar – u 1

sar output

Install :

$ apt-get install atsar

There is no more “sun”

sun.com now redirects to oracle.com

previously:

sun.com hompeage

now:

sun.com redirects to oracle.comAlso blog.sun.com now says Oracle blogs

The end of an era!

how to print tabular data using bash

Looking to print tabular data?

Rows and rows of data in a neat-formatted table-like pattern in BASH?

“echo” botching up tabular data display?

Look no further than “printf” command in BASH

printf: usage: printf [-v var] format [arguments]

example :

$ printf “%20s %40s\n” “data 01″ “data 02″