I have the following code that pulls the number of downloads someone has completed from a database.
Ideally, the way this is supposed to work is that the user is allowed 5 downloads for a period. If the user has more than 5 downloads, they are denied the download and sent to another page. If they are allowed to download, the download proceeds. It also increments a counter field in the database. This is the code:
//Pull user id from cookie
$xnum = $HTTP_COOKIE_VARS['bbuserid'];
//Get number of downloads for current member
$query = "SELECT usergroupid, filedownloads FROM user WHERE userid = $xnum;";
$result = mysql_query($query);
$total_downloads = mysql_fetch_array($result);
$member_usergroup = $total_downloads[usergroupid];
$member_downloads = $total_downloads[filedownloads];
$pafiledb_sql->connect($db);
$config = $pafiledb_sql->query($db,"SELECT * FROM $db[prefix]_settings",1);
//If the user has a Monthly Membership
if ($member_usergroup == 10) {
if ($member_downloads == 5) { //if downloads are 5
//Deny user download and redirect to upgrade page
chdir("c:/phpdev/www/html/modules/forums");
?>
<body onload=javascript:window.location.href="../../../segue.html">
<?php
exit;
} else { //if downloads are less than 5
//Allow user to download file
include "./includes/download.php";
//Increment file counter by 1
$member_downloads = $member_downloads + 1;
//Update file counter field in database
$query = "UPDATE user SET filedownloads = $current_downloads WHERE userid = $xnum;";
$result = mysql_query($query);
}
} else {
include "./includes/download.php";
}
But of course, things are not as they should be and I'm running into the following problems:
When a user downloads the file, If the user has reached their 5 file limit, the download is still proceeding. It should not allow them to download the file.
There is a line that increments the counter by one. What I'm finding though, is that the field in the database gets set to whatever number I am incrementing by, instead of adding that number and the current file download number together to get the new total. For instance, if I changed my increment number to 3, once I re-run the script, the new number in the database would change to 3, no matter how many downloads were completed by the user.
Any ideas? I've been toying with this for hours now, and what seems like it should have been an easy task is now making me feel really stupid. 🙁 Help!