Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script running twice?

Tags:

php

mysql

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';
    }

}

?>
like image 777
Jiri Krewinkel Avatar asked Mar 22 '26 09:03

Jiri Krewinkel


2 Answers

} else if ($count = 0) {

Change that to ==. Looks like you're assigning 0 to the variable count on each loop, which will probably be the cause of your woes.

There's another issue here:

} else if ($count = 1) {

Make sure all of your if statements use == (or ===) to compare, rather than = to assign.

like image 99
turbonerd Avatar answered Mar 24 '26 23:03

turbonerd


Take a look at the server logs to see if you are actually getting two requests on the download URL? I have seen cases where certain browsers (particularly mobile browsers) actually make a HEAD request before doing the actual GET. If your code cannot differentiate between these two request types, it will execute twice.

like image 23
Mike Brant Avatar answered Mar 25 '26 00:03

Mike Brant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!