dtxb75622 2012-07-23 08:28
浏览 50
已采纳

脚本运行两次?

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:

  1. <?php
  2. include ('../storescripts/connect_to_mysql.php');
  3. // Looks first if the post variables have been set
  4. if(!isset($_GET['id']) && ($_GET['lcn'])){
  5. // Error output
  6. echo 'The big PHP monster will not accept you into his cave without bringing an offering of variables!';
  7. } else {
  8. // Set the variables
  9. $id = $_GET['id'];
  10. $license_number = $_GET['lcn'];
  11. // Check if there is such a thing (Yes, aliens) as the given id and license number
  12. $sql = mysql_query("SELECT * FROM secure_downloads WHERE product_id ='$id' AND license_number ='$license_number' LIMIT 1");
  13. $result = mysql_num_rows($sql);
  14. if($result > 0){
  15. // Now update the download count
  16. // Check first if the count is 0
  17. // Make a variable from the count sql
  18. $sql_count = mysql_query("SELECT * FROM secure_downloads WHERE product_id='$id' AND license_number='$license_number' LIMIT 1");
  19. while($row = mysql_fetch_assoc($sql_count)){
  20. $count = $row['count'];
  21. }
  22. // Check if the count is above two
  23. if ($count >= 2){
  24. // Download has already been downloaded 2 times, do not allow download
  25. echo 'The download limit for this file has been reached.';
  26. exit();
  27. } else if ($count = 0) {
  28. // Everything is alright, start downloading
  29. // Force the file download
  30. $file = 'test.jpg';
  31. // Change the count to 1
  32. mysql_query("UPDATE secure_downloads SET count=1 WHERE product_id = '$id' AND license_number = '$license_number'");
  33. readfile($file);
  34. exit();
  35. } else if ($count = 1) {
  36. // Everything is alright, start downloading
  37. // Force the file download
  38. $file = 'test.jpg';
  39. // Change the count to 2
  40. mysql_query("UPDATE secure_downloads SET count=2 WHERE product_id = '$id' AND license_number = '$license_number'");
  41. header('Content-Description: File Transfer');
  42. header('Content-Type: application/octet-stream');
  43. header('Content-Disposition: attachment; filename='.basename($file));
  44. header('Content-Transfer-Encoding: binary');
  45. header('Expires: 0');
  46. header('Cache-Control: must-revalidate');
  47. header('Pragma: public');
  48. header('Content-Length: ' . filesize($file));
  49. ob_clean();
  50. flush();
  51. readfile($file);
  52. exit();
  53. }
  54. } else {
  55. // It doesn't exist, tell the user either the variables were wrong or the
  56. // download limit has been reached
  57. echo 'Cannot download the file, either the link is wrong or the download limit has been reached';
  58. }
  59. }
  60. ?>

展开全部

  • 写回答

3条回答 默认 最新

  • duanqian3953 2012-07-23 08:37
    关注
    } 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 putty实现机器学习猫狗识别
  • ¥15 STS/eclipse导入gradle项目时报错如下
  • ¥15 centos7.6进不去系统,卡在数字7界面
  • ¥15 Tensorflow采用interpreter.allocate_tensors()分配内存出现ValueError: vector too long报错
  • ¥15 使用CGenFF在线生成血红素辅基拓扑结构遇到问题
  • ¥20 matlab代码实现可达矩阵形成骨骼矩阵
  • ¥15 关于地板的木纹和图库中的匹配的
  • ¥30 机器学习预测疾病模型流程疑问
  • ¥50 2048Python实现
  • ¥15 使用ads进行低噪放仿真没有结果且不报错
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部