脚本运行两次?

时间:2012-07-23 16:28:49

标签: 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';
    }

}

?>

3 个答案:

答案 0 :(得分:3)

} else if ($count = 0) {

将其更改为==。看起来你正在为每个循环上的变量0分配count,这可能是造成你的困境的原因。

这里还有另一个问题:

} else if ($count = 1) {

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

答案 1 :(得分:1)

查看服务器日志,看看您是否真的在下载URL上收到两个请求?我已经看到某些浏览器(特别是移动浏览器)在进行实际GET之前实际发出HEAD请求的情况。如果您的代码无法区分这两种请求类型,它将执行两次。

答案 2 :(得分:0)

在我的情况下,它是关于favicon.ico的缺失路径参考。 href =“/ favicon.ico”(好)vs href =“favicon.ico”(不好)。

Mike的答案下面的Ignas评论有所帮助,但在日志中已经确认。缺少的路径引用导致Apache2 mod_rewrite将/ controller / view / id映射为/controller/view/favicon.ico,这当然导致了日志中的一系列PHP错误,例如“PHP注意:未定义的索引:foo.php中的bar第83行“。我知道索引存在,我可以在我面前看到打印出的值。

帮助他人调试/确认问题的一些工具。

<?php error_log($_SERVER['REQUEST_URI']); ?>

使用命令行跟踪错误。例如

$ tail -F /var/log/apache2/error.log

刷新,你会在日志中注意到这样的事情两次(可能更多?)。

示例:

[Thu Jan 11 03:20:44.211993 2018] [:error] [pid 21161] [client 192.168.0.1:34254] /controller/view/11, referer: http://localhost/controller/view/11

**NOTE: NO ERRORS HERE**

[Thu Jan 11 03:20:44.211993 2018] [:error] [pid 21161] [client 192.168.0.1:34254] /controller/view/favicon.ico, referer: http://localhost/controller/view/11

**NOTE: ERRORS START HERE** The line above is the culprit.

[Thu Jan 11 03:20:44.211993 2018] [:error] [pid 21161] [client 192.168.0.1:34254] PHP Notice:  Undefined index: bar in foo.php line 83

[Thu Jan 11 03:20:44.211993 2018] [:error] [pid 21161] [client 192.168.0.1:34254] PHP Notice:  Undefined index: bar2 in foo.php line 84

[Thu Jan 11 03:20:44.211993 2018] [:error] [pid 21161] [client 192.168.0.1:34254] PHP Notice:  Undefined index: bar3 in foo.php line 85

这是一个愚蠢的错误,但修复起来并不好玩。 +1用于在处理代码时拖尾日志文件。感谢Ignas和Mike。这是一个较老的问题,但它是Google的唯一堆栈答案,“php脚本执行两次”所以我认为我会贡献并扩展其他两个想法。