使用此代码,我想将其转换为当用户单击按钮时,它首先验证他们是否要下载文件,如果单击是,则从 $userpoints 中减去 100 分(然后更新 mysql 表,但我知道如何做到这一点),我需要帮助的是让代码在有人单击按钮时运行。此外,我想给他们下载,而不让他们获得可以重复使用的 url。要么不向他们显示 url,要么使其成为唯一的一次性代码。
<?php
//If submit form was clicked
if(isset($_POST['submit'])) {
//Server side validation for security purposes
if($userpoints >= 100) {
mysqli_query($con,"UPDATE users SET points = points - 100 WHERE users.user_name = '$username' LIMIT 1");
$filename = 'ibelongwithyou.mp3';
$filepath = '/home4/saintfiv/downloads/';
header("Content-Length: ".filesize($filepath.$filename));
header("Content-Disposition: attachment; filename=I Belong With You.mp3");
readfile($filepath.$filename);
}else {
header("Location: index.php?error=1");
}
}
?>
<form method="post" action="index.php">
<?php
if ($userpoints >= 100) {
echo '<input type="submit" name="submit" value="100pts">';
} else {
echo '<input type="submit" name="submit" value="100pts" disabled title="You need at least 100 points for this download">';
}
if(isset($_GET['error']) && $_GET['error'] == 1) {
echo "Error, you must have atleast 100points";
}
?>
</form>
使用此更新的代码,刷新后似乎仍在注册单击提交。还有一些奇怪的事情发生在一堆随机字符上,这些字符一直在页面上向下移动。