Why is my PHP request executed twice when the page loads?

I'm new to PHP, and I seem to have a problem with the insert statement, which runs twice when I open this page to view the document. The document is displayed without errors. In the database, the second insert is 1 second later. This only happens on Google Chrome and only on this page. IE has no problems, I have no firefox validation.

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('Ymd 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> 

My HTTP Access Entries 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 checked my links <img src=''> and I don't see a problem with them.

The entries indicate that there is a favicon.ico request, so I created a blank icon and placed it in my public_html folder and linked it on the page, for example <link href="../favicon.ico" rel="shortcut icon" type="image/x-icon" />

Unfortunately, this did not work, as the statement still executes twice. I am not sure if this is favicon problem, since my download page uses insert request and is executed once.

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

+4
source share
2 answers

This code:

 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); //... } 

must exit () at the end, for example:

 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); } 

otherwise, you send the file first and then output the HTML after it, causing all kinds of weird behavior. I do not know if this is the reason, but try.

(Also, as a rule, you will need to use something with code 404 if the file is not found ...)

+2
source

This is a very old question, but with the same problem. This is caused by the type application / pdf and the fact that it opens in a browser (using chrome). If you right-click and save the link as, your code will be run only once

The same thing if you change the type to any other. This is simply due to opening pdf in browser windows. No idea of ​​a workaround

+1
source

Source: https://habr.com/ru/post/1446189/


All Articles