为什么我的PHP查询在页面加载时执行两次?

时间:2012-11-15 10:56:18

标签: php mysql insert

我对PHP很新,我似乎遇到了一个插入语句的问题,当我打开此页面查看文档时,该语句执行两次。该文档显示没有错误。在数据库中,第二次插入是1秒后。它仅在Google Chrome中发生,仅在此页面上发生。 IE没有问题,我没有火狐来检查。

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%">&nbsp;</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="shortcut icon" type="image/x-icon" />

不幸的是,由于语句仍然执行两次,因此无效。 我不确定它是否是一个favicon问题,因为我的上传页面使用插入查询并且它执行一次。

如果有人可以告诉我哪里出错了或者指出我正确的方向我会非常感激

3 个答案:

答案 0 :(得分:2)

此代码:

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的东西......)

答案 1 :(得分:1)

这是一个非常古老的问题,但只是有同样的问题。它是由application / pdf类型引起的,以及它在浏览器中打开的事实(使用chrome)。如果右键单击并保存链接,则代码将仅运行一次

如果您将类型更改为任何其他类型,则相同。这只是因为在浏览器窗口中打开pdf。不知道解决方法

答案 2 :(得分:0)

详细说明以前的答案和评论.....

Wordpress 导致 favicon.ico 加载第二个页面。我从服务器日志中发现了这一点,并将 S_SERVER 变量附加到日志文件中。因此,为了 100% 结束,我在不想运行两次的代码之前添加了以下代码(在我的类中的函数内)

if (isset($_SERVER['REDIRECT_SCRIPT_URI']))
            {
                if ($_SERVER['REDIRECT_SCRIPT_URI']==="https://whaterver.yoursite.com/favicon.ico")
                    return;
            }

……还有多田。问题消失了。 whaterver.yoursite.com 显然是您的网站网址。