为什么我的PHP查询在页面加载时执行两次?
发布时间:2020-12-13 16:48:20 所属栏目:PHP教程 来源:网络整理
导读:我对 PHP很新,我似乎遇到了一个insert语句的问题,当我打开这个页面来查看文档时,它会执行两次.该文档显示没有错误.在数据库中,第二次插入是1秒后.它仅在Google Chrome中发生,仅在此页面上发生. IE没有问题,我没有firefox检查. view_document.php ?phprequire
我对
PHP很新,我似乎遇到了一个insert语句的问题,当我打开这个页面来查看文档时,它会执行两次.该文档显示没有错误.在数据库中,第二次插入是1秒后.它仅在Google Chrome中发生,仅在此页面上发生. IE没有问题,我没有firefox检查.
view_document.php <?php require_once($_SERVER['DOCUMENT_ROOT'] . '/../includes/core.php'); require_once($_SERVER['DOCUMENT_ROOT'] . '/../includes/connect.php'); $webusername = $_SESSION['webname']; if (isset($_GET['document'])) { $ainumber = (int) $_GET['document']; if (!ctype_digit($_GET['document']) || !preg_match('~^[0-9]+$~',$_GET['document']) || !is_numeric($_GET['document'])) { $_SESSION = array(); session_destroy(); header('Location: login.php'); } else { $stmt = $connect->prepare("SELECT s_filename,s_reference FROM dmsmain WHERE s_ainumber = ?") or die(mysqli_error()); $stmt->bind_param('s',$ainumber); $stmt->execute(); $stmt->bind_result($filename,$reference); $stmt->fetch(); $stmt->close(); $file = $_SERVER['DOCUMENT_ROOT'] . '/../dms/files/' . $filename . '.pdf'; if (file_exists($file)) { header('Content-Type: application/pdf'); header('Content-Disposition: inline; filename=' . basename($file)); header('Content-Transfer-Encoding: binary'); header('Content-Length: ' . filesize($file)); header('Accept-Ranges: bytes'); readfile($file); $stmt = $connect->prepare("INSERT INTO dmslog (s_reference,s_userid,s_lastactivity,s_actiontype) VALUES (?,?,?)") or die(mysqli_error()); date_default_timezone_set('Africa/Johannesburg'); $date = date('Y-m-d H:i:s'); $actiontype = 'DL'; $stmt->bind_param('ssss',$reference,$webusername,$date,$actiontype); $stmt->execute(); $stmt->close(); } else { $missing = "<b>File not found</b>"; } exit(0); // Correct Place? } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="../CSS/dms.css" rel="stylesheet" type="text/css" /> <link href="../favicon.ico" rel="icon" type="image/x-icon" /> <title>Denso Document Manager - View Document</title> </head> <body> <div id="container"> <div id="header"> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="28%"><img src="../images/Logo.gif"/></td> <td width="43%"><h2 style="color:#666" align="left">Denso SA Document Management System</h2></td> <td width="3%"> </td> <td width="25%" align="right"><?php echo "Welcome $webusername <input class="look" type="button" onclick="parent.location='logout.php'" value=' Logout ' />" ?></td> </tr> </table> <br /> <table bgcolor="#6699CC" height="30px" width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="20%" align="center"><p style="color:#FFF"><b>Document Search</b></p></td> <td width="20%" align="center"><p style="color:#FFF"><b>Add Document</b></p></td> </tr> </table> </div> <div id="content"> <?php echo $missing; ?> <br /> <br /> </div> <div id="footer"> <table width="100%" bgcolor="#6699CC"> <tr> <td height="25px"></td> </tr> </table> <table width="100%" bgcolor="#000000"> <tr> <td height="25px"></td> </tr> </table> </div> </div> </body> </html> 我假设我的HTTP访问记录 [15/Nov/2012:10:14:32 +0200] "POST /dms/search.php HTTP/1.1" 200 5783 "http://www.denso.co.za/dms/search.php" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML,like Gecko) Chrome/23.0.1271.64 Safari/537.11" [15/Nov/2012:10:14:33 +0200] "GET /favicon.ico HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML,like Gecko) Chrome/23.0.1271.64 Safari/537.11" [15/Nov/2012:10:14:34 +0200] "GET /dms/view_document.php?document=8 HTTP/1.1" 200 2965 "http://www.denso.co.za/dms/search.php" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML,like Gecko) Chrome/23.0.1271.64 Safari/537.11" [15/Nov/2012:10:14:35 +0200] "GET /favicon.ico HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML,like Gecko) Chrome/23.0.1271.64 Safari/537.11" 我检查了我的< img src =''>链接,我没有看到他们的问题. 记录表明有一个favicon.ico请求所以我创建了一个空白的favicon并将其放在我的public_html文件夹中,并将其链接到页面中,如此< link href =“../ favicon.ico”rel =“快捷图标” type =“image / x-icon”/> 不幸的是,由于声明仍然执行两次,因此无效. 如果有人能告诉我我哪里出错了或指出我正确的方向,我将非常感激 解决方法
这段代码:
if (file_exists($file)) { header('Content-Type: application/pdf'); header('Content-Disposition: inline; filename=' . basename($file)); header('Content-Transfer-Encoding: binary'); header('Content-Length: ' . filesize($file)); header('Accept-Ranges: bytes'); readfile($file); //... } 绝对应该在它的末尾有exit()调用,如下所示: if (file_exists($file)) { header('Content-Type: application/pdf'); header('Content-Disposition: inline; filename=' . basename($file)); header('Content-Transfer-Encoding: binary'); header('Content-Length: ' . filesize($file)); header('Accept-Ranges: bytes'); readfile($file); //... exit(0); } 否则你首先发送文件,然后输出HTML,导致各种奇怪的行为.不知道是不是原因,但试试吧. (另外,作为一个原则,如果找不到文件,你需要用代码404提供服务……) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |