• Increase font size
  • Default font size
  • Decrease font size

dynamic download link

Once, I had to face problem, how to generate download link dynamicly. I did not want everybody could download my files by copy pasting link into browser. Here is the solution I have created:

First we need a regular download link. This link is directed to the php script which handles whole process of downloading. Two variables are send to the php script by "GET".

<?php
//uses hash of date to ensure security
$password = "pass";
$strToHash = date('m.d.y').$password;
$hash = hash('md5', $strToHash);
echo "<a href='dl.php?filename=icon.ico&id={$hash}'>working DOWNLOAD</a><br><br>";
echo "<a href='dl.php?filename=icon.ico&id=1'>not working DOWNLOAD</a>";
?>

  1. filename which we want to download
  2. id which is our MD5 hash generated based on the date and a password.
Everythig looks like this:

As you can see, hypertext runs dl.php script. This script has two tasks:

  1. control if it was run by our link. It is done by the hash control
  2. allow us to download the requested file (defined by filename variable)
Here is the code:
<?php
$download_path = "C:\\";    //directory where downloadable files are stored
$password = "pass";         //password
$filename = $_GET['filename'];
$hash1 = $_GET['id'];
$strToHash = date('m.d.y').$password;
$hash2 = hash('md5', $strToHash);
if(strcmp($hash1, $hash2) != 0){
die("Please try again later.");
}
// Detect missing filename
if(!$filename) die("I'm sorry, you must specify a file name to download.");
// Make sure we can't download files above the current directory location.
if(eregi("\.\.", $filename)) die("I'm sorry, you may not download that file.");
$file = str_replace("..", "", $filename);
// Make sure we can't download .ht control files.
if(eregi("\.ht.+", $filename)) die("I'm sorry, you may not download that file.");
// Combine the download path and the filename to create the full path to the file.
$file = "$download_path$file";
// Test to ensure that the file exists.
if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
// Extract the type of file which will be sent to the browser as a header
$type = filetype($file);
// Send file headers
header("Content-type: $type");
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
// Send the file contents.
set_time_limit(0);
readfile($file);
?>

Source code, you may download here.

 

 

Comments  

 
0 #207 joker stash new link 2018-11-13 05:36
I was very pleased to discover this great site. I wanted to
thank you for ones time due to this fantastic read!! I definitely appreciated every little bit of it and i also have
you bookmarked to look at new things on your site.
Quote
 
 
0 #206 sky777 2018-11-12 05:17
Here are five anyone may try to optimize site. There
has been a lot of fuss about SEO or search engine optimization.
If used correctly, blogs get throughout the internet through feeds.
Quote
 
 
0 #205 BestBenito 2018-11-06 07:01
I have noticed you don't monetize your site,
don't waste your traffic, you can earn additional cash every month.
You can use the best adsense alternative for any type
of website (they approve all websites), for more info simply search in gooogle: boorfe's
tips monetize your website
Quote
 
 
0 #204 mega casino slots 2018-11-02 17:12
For just about all the people, making money is the first thing for to be
able to build a webpage. Not every one is worthwhile you will find many blog.
Marketing site is not every that difficult to
do.
Quote
 
 
0 #203 ace333 slot 2018-11-02 08:45
I read this paragraph fully regarding the difference of
newest and earlier technologies, it's remarkable article.
Quote
 
 
0 #202 3win8 slot 2018-11-01 23:38
It's remarkable designed for me to have a site, which is
valuable in favor of my knowledge. thanks admin
Quote
 
 
0 #201 3win8 download 2018-11-01 16:07
Visit various forums and chat rooms to gather more about your market.
As frequently say, it isn't always being aware what to write,
but the way to write it.
Quote
 
 
0 #200 millions lottery 2018-10-29 16:53
Very great post. I just stumbled upon your weblog and wanted
to mention that I have truly enjoyed surfing around your blog
posts. After all I will be subscribing on your feed and
I am hoping you write again soon!
Quote
 
 
0 #199 jstash cvv 2018-10-13 20:56
Superb site you have here but I was curious about if
you knew of any forums that cover the same topics discussed here?

I'd really love to be a part of group where I can get comments
from other experienced individuals that share the same interest.
If you have any recommendations , please let me know.

Appreciate it!
Quote
 
 
0 #198 remy hair 2018-10-10 16:49
After looking into a handful of the articles on your web site, I
seriously appreciate your way of blogging. I bookmarked it to my bookmark site list and will be checking back in the near future.
Please check out my website as well and tell
me your opinion.
Quote
 
 
0 #197 tips to find jobs 2018-10-10 15:22
I do not even knoѡ how I ended up here, but І thought this post was grеat.
Ι do not ҝnow whߋ you are but certainly yoᥙ're goіng to
a famous blogger іf yoս are not aⅼready ;) Cheers!
Quote
 
 
0 #196 Marguerite 2018-09-22 09:01
1.845). LASCH, Christopher. 1 p. 2, 2003. E também
vice-versa.
Quote
 
 
0 #195 iworking.vn 2018-09-13 09:09
I like this site іt's a master piece! Glad Ι discovered tһіs
on google.
Quote
 
 
0 #194 ArleneJuicy 2018-09-07 11:19
Hello. I see that you don't update your blog too often.
I know that writing articles is boring and time consuming.

But did you know that there is a tool that allows you to create
new posts using existing content (from article directories or other pages from your niche)?
And it does it very well. The new articles are high quality and pass the copyscape test.
Search in google and try: miftolo's tools
Quote
 

Add comment

No bad words.


Security code
Refresh


New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 2001 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2831 Hits
Inverse kinematics Machine Learning and Robotics
Read More 3784 Hits
gpsim tutorial Machine Learning and Robotics
Read More 9247 Hits

Most read content

A* search algorithm Machine Learning and Robotics
Read More 12799 Hits
gpsim tutorial Machine Learning and Robotics
Read More 9247 Hits

New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 2001 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 2831 Hits
Inverse kinematics Machine Learning and Robotics
Read More 3784 Hits
gpsim tutorial Machine Learning and Robotics
Read More 9247 Hits
Design by i-cons.ch / etosha-namibia.ch