php – 表单提交w /验证后提供下载
作者:互联网
我在.php中创建了一个非常简单的下载代码救赎者(感谢此处的帮助),并且我很难弄清楚如果验证成功,下载服务的最佳方式是什么.基本上 –
用户输入无效代码 – >页面刷新并显示错误消息.
用户输入有效代码 – >下载“另存为” – >刷新页面.
在我使用http://www.zubrag.com/scripts/download.php来提供文件的那一刻,但是一旦开始下载,我的表单刷新页面,但只有一半加载内容?!
这是我用PHP脚本的表单.
<div class="dcrForm">
<p>Have a physical copy of this release? Claim your digital download by entering your Download Code below.</p>
<form action="index.php" method="post">
<input type="text" name="code" class="dcrInput" value="">
<input type="submit" name="harrisSubmit" class="dcrSubmit" value="Submit">
</form>
<?php
include("scripts/dcr_config.php");
$code="";
$log="";
if (isset($_POST['harrisSubmit']))
{
$code=$_POST['code'];
$link = mysql_connect($hostname, $dbusername, $dbpassword);
mysql_select_db("$databasename");
$query = "select count from $harris where code='$code'";
if ($q=mysql_query($query))
if ($r=mysql_fetch_array($q)){
if ($r[0]<3)
{
$subquery="update $tbname set count='".($r[0]+1)."' where code='$code'";
mysql_query($subquery);
?><script>window.location.href="download.php?f=test.txt";</script><?php
}
}
$log="<p>Invalid code. Try Again.</p>";
}
echo $log."";
?>
</div>
有没有人对下载服务的最佳方式有什么想法?我知道目前任何拥有该文件位置的人都可以下载该文件,但我不知道如何保护我
解决方法:
我很高兴你能做到这一点!
如果您要将用户重定向到下载脚本,则该脚本需要附加某种令牌以防止未经授权的下载,基本上会重新验证给定的代码或令牌.
在上面的脚本中,您可以执行以下操作,而不是输出javascript以重定向到下载脚本:
<?php
include "scripts/dcr_config.php";
$code = "";
$log = "";
if (isset($_POST['harrisSubmit'])) {
$code = trim($_POST['code']);
$link = mysql_connect ( $hostname, $dbusername, $dbpassword );
mysql_select_db ( "$databasename" );
$code = mysql_real_escape_string($code); // very important! protects against exploits
$query = "select count from $harris where code='$code'";
if ($q = mysql_query ( $query )) {
if ($r = mysql_fetch_array ( $q )) {
if ($r [0] < 3) {
$subquery = "update $tbname set count='" . ($r [0] + 1) . "' where code='$code'";
mysql_query ( $subquery );
$file = '/path/to/protecteddownload.txt';
// send file to browser as a download dialog
// no content can be output prior to these header() calls
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="file.txt"');
header('Content-Length: ' . filesize($file));
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
echo file_get_contents($file);
exit; // terminate script
} else {
$log = 'Sorry, this code has already been redeemed.';
}
} else {
$log = 'Invalid download code. Try again.';
}
} else {
// query failed
$log = 'An error occurred validating your code, please try again later.';
}
$log = "<p>Invalid code. Try Again.</p>";
}
?>
<?php if (isset($log) && $log != ''): ?>
<strong class="error"><?php echo $log ?></strong>
<?php endif; ?>
<div class="dcrForm">
<p>Have a physical copy of this release? Claim your digital download by
entering your Download Code below.</p>
<form action="index.php" method="post"><input type="text" name="code"
class="dcrInput" value=""> <input type="submit" name="harrisSubmit"
class="dcrSubmit" value="Submit"></form>
</div>
下载脚本可能类似于我上面的一些内容.
关于此示例的关键是,无法从Web访问您使用file_get_contents提供的文件.您只能在输入有效代码时发送它.
标签:php,http-headers,download,page-refresh 来源: https://codeday.me/bug/20190521/1147538.html