Php 脚本运行两次?
好的,我正在尝试建立一个安全的下载系统,在这个系统中,一个拥有特定许可证号码的买家可以访问一个下载,在他的许可被取消之前,他可以下载两次。为了做到这一点,我在同一行的“产品id”和“许可证编号”列旁边有一个“计数”列。当我的paypal ipn脚本确认时,产品id和许可证号将自动生成并传递给买家 现在问题来了:当他们使用正确的变量访问下载页面时,计数会更新为+1,但由于某种原因,这个sql查询会运行两次,而我的数据库中实际上会出现+2。我已经对它做了一些修改,先检查值,然后进行相应的更改(看看这是否修复了错误),但错误仍然没有修复 我个人认为,也许我调用一个文件下载会使脚本运行两次,或者我错了吗 代码如下:Php 脚本运行两次?,php,mysql,Php,Mysql,好的,我正在尝试建立一个安全的下载系统,在这个系统中,一个拥有特定许可证号码的买家可以访问一个下载,在他的许可被取消之前,他可以下载两次。为了做到这一点,我在同一行的“产品id”和“许可证编号”列旁边有一个“计数”列。当我的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';
}
}
?>
查看服务器日志,看看下载URL上是否有两个请求?我见过某些浏览器(特别是移动浏览器)在执行实际GET之前实际发出HEAD请求的情况。如果您的代码无法区分这两种请求类型,它将执行两次
} else if ($count = 0) {
将其更改为==
。看起来您正在为每个循环上的变量count
赋值0
,这可能是您的问题所在
这里还有一个问题:
} else if ($count = 1) {
确保所有的if
语句都使用=
(或=
)进行比较,而不是=
进行分配。在我的例子中,favicon.ico上缺少路径引用,即href=“/favicon.ico”(好)vs href=“favicon.ico”(坏)
迈克回答下面的伊格纳斯评论有所帮助,但日志中证实了这一点。缺少路径引用导致Apache2 mod_重写为map/controller/view/id as/controller/view/favicon.ico,这当然会导致日志中出现大量PHP错误,如“PHP注意事项:foo.PHP第83行中未定义的索引:bar”。我知道索引存在,我可以看到打印出来的值就在我面前
一些工具可以帮助其他人调试/确认问题
<?php error_log($_SERVER['REQUEST_URI']); ?>
刷新后,您会在日志中两次(可能更多?)注意到类似的内容
例如:
[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用于在处理代码时跟踪日志文件。也要感谢伊格纳斯和迈克。这是一个较老的问题,但这是谷歌搜索“php脚本执行两次”时唯一的堆栈答案,所以我想我会对其他两个想法有所贡献并加以扩展。IPN是否将其帖子发送到同一个脚本?这就是为什么您应该使用POST来执行可以更改服务器状态的操作的原因之一。浏览器可以自由地假设GET请求不会导致任何可观察到的状态更改,因此,如果他们愿意,可以自由地多次请求此类页面。@yoavmatchulsky会的,但是到目前为止,这只是一个独立的脚本im testing+1,我遇到了同样的问题,浏览器不断地向/favicon.ico文件发出第二个请求,而该文件并不存在。但因为我使用mod_rewrite将所有内容重写到index.php,所以得到了双重请求。这就解决了问题!真不敢相信我竟然忽略了!那么,我能得到快乐的绿色记号吗D
[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