Password Protected Download
Posted: Tue Jun 27, 2006 7:58 am
Pimptastic | Please use
How would I adapt this to check if a correct password was entered, and if not how would I redirect them to a failed page?
Also this is my form code, how do I redirect the URL to the download.php page?
Cheers
Pimptastic | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi,
I have a page that has an input box on it for a password. When you press submit, I want this to download a file, but only if it is a correct password.
Here is my download code:Code: Select all
<?php
// format http://www.yoursite.com/download.php?file=filepath or relative path
$filename = $_GET['file'];
// get the file extention
$file_extension = strtolower(substr(strrchr($filename,"."),1));
// if no filename given ie: someone accessing the page directly
if( $filename == "" )
{
echo "File not given";
exit;
} elseif ( ! file_exists( $filename ) )
{
echo "File does not exist";
exit;
};
// switch the file extention to get the right type
switch( $file_extension )
{
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
// send the headers to the browser
header("Pragma: public"); // required
header("Expires: 0");
// don't cache
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// same as above
header("Cache-Control: private",false); // required for certain browsers
// get content type
header("Content-Type: $ctype");
// set as attatchment and name the filename //
// basename takes just the filename without any slashes etc
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
// set the enc type
header("Content-Transfer-Encoding: binary");
// tell the browser how big the file is
header("Content-Length: ".filesize($filename));
// readfile erm, reads the file!
readfile("$filename");
// end script
exit();
?>Also this is my form code, how do I redirect the URL to the download.php page?
Code: Select all
<form action="download.php" method="get" id="passwordForm">
<p>
<label for="password">Enter Password to download presentation:</label>
</p>
<p>
<input name="password" id="pwd" type="password" />
<input name="submit" id="submit" type="button" value="download file" />
</p>
</form>Pimptastic | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]