Okay so I’m trying to make a secure download system where a certain buyer with a specific license number has access to a download which he can download twice before his permission is lifted. In order to do this I’ve got a ‘count’ column next to a ‘product_id’ and ‘license_number’ column in the same row. The product id and license number are automatically generated and passed onto the buyer when my paypal ipn script confirms it.
Now here’s the problem: when they access the download page with the correct variables the count gets updated by +1, but for some reason this sql query is run twice and I get +2 in my database actually. I have already changed it a bit to check the value first and then change accordingly (to see if that fixes the error) but the error still isn’t fixed.
I personally think that maybe me calling a file to download makes the script run twice or am I wrong here?
This is the code:
<?php
include ('../storescripts/connect_to_mysql.php');
// Looks first if the post variables have been set
if(!isset($_GET['id']) && ($_GET['lcn'])){
// Error output
echo 'The big PHP monster will not accept you into his cave without bringing an offering of variables!';
} else {
// Set the variables
$id = $_GET['id'];
$license_number = $_GET['lcn'];
// Check if there is such a thing (Yes, aliens) as the given id and license number
$sql = mysql_query("SELECT * FROM secure_downloads WHERE product_id ='$id' AND license_number ='$license_number' LIMIT 1");
$result = mysql_num_rows($sql);
if($result > 0){
// Now update the download count
// Check first if the count is 0
// Make a variable from the count sql
$sql_count = mysql_query("SELECT * FROM secure_downloads WHERE product_id='$id' AND license_number='$license_number' LIMIT 1");
while($row = mysql_fetch_assoc($sql_count)){
$count = $row['count'];
}
// Check if the count is above two
if ($count >= 2){
// Download has already been downloaded 2 times, do not allow download
echo 'The download limit for this file has been reached.';
exit();
} else if ($count = 0) {
// Everything is alright, start downloading
// Force the file download
$file = 'test.jpg';
// Change the count to 1
mysql_query("UPDATE secure_downloads SET count=1 WHERE product_id = '$id' AND license_number = '$license_number'");
readfile($file);
exit();
} else if ($count = 1) {
// Everything is alright, start downloading
// Force the file download
$file = 'test.jpg';
// Change the count to 2
mysql_query("UPDATE secure_downloads SET count=2 WHERE product_id = '$id' AND license_number = '$license_number'");
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit();
}
} else {
// It doesn't exist, tell the user either the variables were wrong or the
// download limit has been reached
echo 'Cannot download the file, either the link is wrong or the download limit has been reached';
}
}
?>
Change that to
==. Looks like you’re assigning0to the variablecounton each loop, which will probably be the cause of your woes.There’s another issue here:
Make sure all of your
ifstatements use==(or===) to compare, rather than=to assign.