Why is my PHP query executing twice on page load?

Posted by user1826238 on Stack Overflow See other posts from Stack Overflow or by user1826238
Published on 2012-11-15T10:56:17Z Indexed on 2012/11/15 11:00 UTC
Read the original article Hit count: 435

Filed under:
|

I am newish to PHP and I seem to be having an issue with an insert statement that executes twice when I open this page to view a document. In the database the 2nd insert is 1 second later. It happens in google chrome only and on this page only. IE has no issue, I dont have firefox to check.

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>";

}
}
}

?>

My HTTP access records I assume

[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"

I have checked my <img src=''> links and I dont see a problem with them.

The records indictate there is a favicon.ico request so I created a blank favicon and placed it in my public_html folder and linked it in the page like so <link href="../favicon.ico" rel="shortcut icon" type="image/x-icon" />

Unfortunately that did not work as the statement still executes twice. I am unsure if it is a favicon issue as my upload page uses an insert query and it executes once.

If someone could please tell me where I am going wrong or point me in the right direction I would be very grateful

© Stack Overflow or respective owner

Related posts about php

Related posts about insert