A non-javascript pathway is for people without javascript enabled. For them colorbox won't open, and they will be taken directly to the login page where they could still download the file if they have the correct password, things just wouldn't be as neat and tidy. I don't have that fully worked out yet, but I think it's possible from some of the things that happened when I was building the javascript assited version with colorbox.
So I've got a working demo on my local PHP server. I don't have access to a live PHP server at the moment , so I'll give you the files. I used ColorBox v1.3.16 and from the demo archive example1 I changed its example7 link:
Code:
<a class='example7' href="http://google.com">Outside Webpage (Iframe)</a>
to:
Code:
<a class='example7' href="index_3.php">Login (Iframe)</a>
Here's the code for index_3.php:
Code:
<?php
$passwords['joesentme'] = '/server/pdf/pdf_1.pdf';
$passwords['bobsyeruncle'] = '/server/pdf/pdf_2.pdf';
$thePass = isset($_POST['txtPassword'])? $_POST['txtPassword'] : '';
$approved = false;
?>
<!DOCTYPE html>
<html>
<head>
<title>Login for Files</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
if (array_key_exists($thePass, $passwords)){
$approved = true;
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
jQuery(function($){
$('body').append('<iframe style="display: none;" src="download.php?txtPassword=<?php echo $thePass; ?>" width="0" height="0" scrolling="auto" frameborder="1"></iframe>');
setTimeout(function(){parent.jQuery.colorbox.close();}, 3000);
});
</script>
<?php
}
?>
<style type="text/css">
#required, #wrong {
color: red;
font: bold 90% sans-serif;
}
</style>
</head>
<body>
<h1>Login for Files</h1>
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>Password:
<br><input type="password" title="Enter your password" name="txtPassword"></p>
<script type="text/javascript">
document.write('<input type="hidden" name="scripted" value="scripted">');
</script>
<p><input type="submit" name="Submit" value="Login"> <input type="reset" value="Reset"><br><?php
if(!$approved and isset($_POST['Submit'])){
if( $thePass == ''){
echo ' <span id="required">Password Required!</span>';
} else {
echo ' <span id="wrong">Incorrect Password!</span>';
}
} ?><span> </span>
</p>
</form>
</body>
</html>
<?php
die();
?>
The highlighted is where you configure the passwords and files. For added security I placed mine off the parent of the root where they're inaccessible to the browser without the use of these PHP scripts. They can be anywhere on your server.
Also pay attention to the use of jQuery:
Code:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
For best results this will point to the same copy of jQuery as is used by the top page, but that's a fine point. It will simply help with load time.
Another file in the same folder which is used is download.php:
Code:
<?php
$passwords['joesentme'] = '/server/pdf/pdf_1.pdf';
$passwords['bobsyeruncle'] = '/server/pdf/pdf_2.pdf';
$thePass = isset($_REQUEST['txtPassword'])? $_REQUEST['txtPassword'] : '';
if (array_key_exists($thePass, $passwords)){
$fullPath = $passwords[$thePass];
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
}
die();
?>
You have to configure the passwords and files in it as well.
That's it, try it out and let me know.
When I have the time, I'll be playing with the non-javascript version and tightening up the code.
Bookmarks