php - 脚本运行两次?

标签 php mysql

好的,所以我正在尝试建立一个安全的下载系统,其中具有特定许可证号的特定买家可以访问下载,他可以在取消许可之前下载两次。为了做到这一点,我在同一行的“product_id”和“license_number”列旁边有一个“count”列。当我的 paypal ipn 脚本确认时,产品 ID 和许可证号会自动生成并传递给买家。

现在的问题是:当他们使用正确的变量访问下载页面时,计数会更新 +1,但由于某种原因,这个 sql 查询运行了两次,而我实际上在我的数据库中得到了 +2。我已经对其进行了一些更改以先检查值然后进行相应更改(以查看是否修复了错误)但错误仍未修复。

我个人认为,也许我调用一个文件进行下载会使脚本运行两次,或者我在这里错了吗?

这是代码:

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

}

?>

最佳答案

} else if ($count = 0) {

将其更改为 ==。看起来你在每个循环中将 0 分配给变量 count,这可能是你遇到麻烦的原因。

这里还有一个问题:

} else if ($count = 1) {

确保所有的 if 语句都使用 ==(或 ===)进行比较,而不是 = 分配。

关于php - 脚本运行两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11616603/

相关文章:

mysql - 求和时间戳返回不正确的值和格式

php - Memcached 存储用户账户

php - 我如何将这个 1471323637 转换为时间

php - preg_match_all( ) 在不同的服务器上表现不同

php - Codeigniter 返回变量并检查它的值

mysql - 通过 R 中的所有表运行 MySQL 查询

php - 选择实体假设相关实体不存在

mysql - DBI - Perl - 记录 MySQL 警告

php - Android 无法正确连接 MySQL 数据库

php - 如何获得两个 dateTime obj 之间的毫秒数?