I have a client that uses a website builder that does not offer the ability to edit the code or access the database but hey want to track the downloads of a file.
I setup a system in which I host the file elsewhere, and their website has an <a>
link to a page on the host site with a very simple script to insert a record to track the download, as well as download the file.
This is the php script in the page on my site, download-agreement.php
:
<?php
$db = new mysqli('localhost', 'username', 'password', 'database');
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
$file_path = 'agreement.pdf';
$stmt = $db->prepare("INSERT INTO `download_log` (`date`) VALUES (CURRENT_TIMESTAMP())");
if ($stmt===false) {
error_log($db->error);
}
$result = $stmt->execute();
if ($result===false) {
error_log($stmt->error);
}
$stmt->close();
$db->close();
error_log("Downloaded: " . date("Y-m-d H:i:s"));
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename=agreement.pdf');
readfile($file_path);
?>
And the link I have placed on their site:
https://www.host.com/theirdirectory/download-agreement.php
If link is visited directly, it works properly - the error_log()
executes and the record is inserted.
If the link is visited after being clicked on their website, only the file downloads. The error_log()
is not executed and the record is not inserted.
I am at a bit of a loss as I cannot even get an error. And thoughts or ideas would be appreciated.
EDIT
This seems to be an issue with Chrome for Desktop (which doesn't really make any sense). This works properly on all browsers except for Chrome for Desktop (it works on chrome for mobile), tested on all with caching disabled on multiple computers.