when I try to download from some servers, I`m redirected to a download.php page, which can contain many things, then the file download starts.
I want to know how to track my downloads using this download.php file. I mean I want to know how to write this file and what are the contents of this file.
You mean you want to have your own download.php page that is like the one used by so many others? Here is one of mine:
header('Content-disposition: attachment;filename=file.js');
header('Content-type: text/javascript');
readfile($_SERVER['DOCUMENT_ROOT'] . '/_js/file.js');
That's pretty much it, this will let you download the file. You have to have the correct filename in readfile() and it usually helps to have the correct content type. There are many, so look up the right one depending upon the file type.
I don't know how to track whether the user actually downloaded the file .. probably have to look at the server logs or something. It would be very easy to track who got to the download.php page, though. Just have the page stored in your DB and write the number of times the page was viewed +1.
Here is a pretty nice list of at least some content-types you can use: http://www.utoronto.ca/web/htmldocs/book/book-3ed/appb/mimetype.html
If you want to see a page before the download starts, this is fairly simple redirection using Javascript:
<script type="text/javascript">
setInterval(function(){
window.location = "http://www.example.com/file.php";
},5000);
</script>
You could easily generate the correct URL using PHP:
window.location = "<?php echo $_GET['download'] ?>";
This presumes that your URL is download.php?download=filename