Current location: Hot Scripts Forums » General Community » Script Requests » How to prevent users to download more than one file at time?


How to prevent users to download more than one file at time?

Reply
  #1 (permalink)  
Old 08-28-07, 05:06 PM
oursvince oursvince is offline
Newbie Coder
 
Join Date: Aug 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
How to prevent users to download more than one file at time?

Hello,

As I would like to save some ram and bandwidth, I'm looking for a script that could prevent users to download more than one file at time on my website.

Does anybody knows one?

Thanks,

Vince
Reply With Quote
  #2 (permalink)  
Old 08-28-07, 11:48 PM
curbview.com's Avatar
curbview.com curbview.com is offline
Junior Code Guru
 
Join Date: May 2006
Posts: 555
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by oursvince View Post
Hello,

As I would like to save some ram and bandwidth, I'm looking for a script that could prevent users to download more than one file at time on my website.

Does anybody knows one?

Thanks,

Vince
A few lines of code in Perl can do this. Are you looking for a Perl-based solution or PHP? I like Perl myself for better control and security.
Reply With Quote
  #3 (permalink)  
Old 08-29-07, 07:19 AM
Boraan's Avatar
Boraan Boraan is offline
Coding Addict
 
Join Date: Jul 2007
Location: Clayton, NC
Posts: 292
Thanks: 0
Thanked 1 Time in 1 Post
Here are a few good measures. First, make the directory non-browsable by placing an index.html file in it. I use either a re-direct back to my home page when they hit the directory, or print a page like this one.

http://www.techdex.net/messaging/dl/echomusic/

Here's the code. (Very simple HTML file), just remember to save it as index.html

Code:
<html>
<head>
<title>Directory Browsing Disabled</title>
</head>

<body>
<h1>Directory Browsing Disabled</h1>
<p>Directory browsing has been disabled by the user.</p>
</body>
</html>
If you have any links to your downloads, remove them and use an app to control what they download. I make my users select the download from a form and let the app manage the download.

Here. This is the form. I call mine download.html. I also require them to use an email address to prevent automated downloads.
Code:
<html>
<head>
<title>Select Your Download</title>
<meta HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta HTTP-EQUIV="Expires" CONTENT="-1">
</head>
<body>
<h1>Choose a file to download</h1>
<form action="cgi-bin/dl.cgi" method="POST">
Your Email Address: <input type="text" size="30" name="email">
<select name="package">
<option value="noselect" selected>Select One</option>
<option value="item1">Item 1</option>
<option value="item2">Item 2</option>
<option value="item3">Item 3</option>
</select>
<input type="Submit" value="Download"></form>

</body>
<meta HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta HTTP-EQUIV="Expires" CONTENT="-1">
</html>
And here is the cgi file called dl.cgi. Make sure to place it in the right directory and chmod 755.

Code:
#!/usr/bin/perl
print "Content-type:text/html\n\n";

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}

if ($FORM{'package'} eq "item1") {
$order = "filename";
}

if ($FORM{'package'} eq "item2") {
$order = "filename";
}

if ($FORM{'package'} eq "item3") {
$order = "filename";
}

# if selection is noselect, then die nice
if ($FORM{'package'} eq "noselect") {
&dienice("You didn't select a file to download. Hit back in your browser and select a file.")
} 

# if selection is blank, then die nice
if ($FORM{'package'} eq "") {
&dienice("You didn't select a file to download. Hit back in your browser and select a file.")
}

# Untaint email string to make sure it's a valid email address or not blank.
if ($FORM{'email'} !~ /[\w\-]+\@[\w\-]+\.[\w\-]+/) {
	&dienice("You did not enter a valid email address.");
}  else {
print <<EndHead;
<html><head><title></title>
<meta http-equiv="Refresh" content="3; URL=http://www.url.com/dl/$order">
</head>
<body>
EndHead

# This is optional. This will let you know when someone downloads a file.
# begin mail subroutine
$mailprog = '/usr/sbin/sendmail';

$recipient = 'your@email.com';
open (MAIL, "|$mailprog -t") or &dienice("Can't access $mailprog!\n");
print MAIL "To: $recipient\n";
print MAIL "From: $FORM{'email'}\n";
print MAIL "Reply-to: $FORM{'email'}\n";
print MAIL "Subject: $FORM{'package'} Download\n\n";

print MAIL <<EndHTML;

Email: $FORM{'email'}
Package: $FORM{'package'}

EndHTML

close(MAIL);
# End mail subroutine

# now print something to the HTML page, usually thanking the person
# for filling out the form, and giving them a link back to your homepage

print <<EndHTML;
<h2>Thank You!</h2>
<p style="font-size: 10pt">Thank you! Your download should begin shortly. If it does not, <a href="#" onclick="window.location='http://www.url.com/dl/$order'" style="font-size: 10pt">click here</a> to download the file manually.
</p>
</body></html>
EndHTML
}

sub dienice {
($errmsg) = @_;
print "<h2>Error</h2>\n";
print "$errmsg<p>\n";
print "</body></html>\n";
exit;
}
You can add as many items as you want in the html file just by adding another option field. <option value="item4">Item 4</option>.

Just remember to add the respective handle for it in the cgi file.
if ($FORM{'package'} eq "item4") {
$order = "filename";
}

filename will be the name of the actual file, item.zip, etc.

One last thing. Make sure the file is in your directory otherwise your user will get an error.

The url http://www.url.com/dl/$order must be the url where your file is located, so if you have a file located in the http://www.wemakewidgets.com/download/ directory, your string will look like this:

http://www.wemakewidgets.com/download/$order

The $order is the file name.

Thirdly, you might consider putting a robots.txt file in your root directory of your site, (where your home page is), to help stop search engines from indexing your file.

robots.txt
Code:
User-agent: *
Disallow: /download/
Disallow: /download.html
The directory you disallow is relative, so you don't need the full url.

So now you have no links to your files, you must enter a valid email address to download a file, you can only download 1 file at a time, your download directory is not world readable, and most search engines won't crawl your download directory or download.html file.

And to top it off the download page is set to expire and use pragma to stop it from being cached and require the client to get it fresh from the server every time.

Hmm. One more thing... You will be notified via email when someone downloads. If you want that feature, just put your real email addy in the recipient spot. If you don't want it, delete the mail subroutine.. I've tagged the start and end so it will be easy... just delete between the tags.
__________________
Dexter Nelson
Techdex Development & Solutions
========================
Internet Marketing For Programmers | Free Market Research in 15 Minutes or Less
My Software: Hotscripts Softpedia software.techdex.net

Last edited by Boraan; 08-29-07 at 07:25 AM. Reason: updated the noselect error function.
Reply With Quote
  #4 (permalink)  
Old 08-29-07, 10:56 AM
oursvince oursvince is offline
Newbie Coder
 
Join Date: Aug 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Great!

Thanks for your time and help.

Quote:
Originally Posted by curbview.com View Post
A few lines of code in Perl can do this. Are you looking for a Perl-based solution or PHP? I like Perl myself for better control and security.
Thank you too.
To be honest, I'm a newbie in programming, generally I let my web editor do that or take ready-made scripts.
If using a Perl-based solution is not too hard to make, I'd like to give a try.

Vince

Last edited by oursvince; 08-29-07 at 11:01 AM.
Reply With Quote
  #5 (permalink)  
Old 08-29-07, 12:26 PM
Boraan's Avatar
Boraan Boraan is offline
Coding Addict
 
Join Date: Jul 2007
Location: Clayton, NC
Posts: 292
Thanks: 0
Thanked 1 Time in 1 Post
Your welcome. The program I put up there is in perl and free for whoever to use it. enjoy!
__________________
Dexter Nelson
Techdex Development & Solutions
========================
Internet Marketing For Programmers | Free Market Research in 15 Minutes or Less
My Software: Hotscripts Softpedia software.techdex.net
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to download file using wget techie82 Script Requests 1 02-13-07 02:27 PM
download jpg file in php tamil PHP 2 01-30-07 03:26 AM
Download not to play a file mp3 from external server Melamania Script Requests 1 12-17-06 05:27 PM
File download protector/Information collector cramerdy HTML/XHTML/XML 1 09-01-05 09:48 PM
Showing time is users time zone? Nasimov PHP 1 11-20-03 07:14 AM


All times are GMT -5. The time now is 09:13 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.