As I understand it, what you want to do is stop the situation where a link to a PDF is accessed by an unauthenticated user. This is typically done by serving the PDF file via a script.
The PDF files should be stored in a directory outside the public web root, however if you do this, make sure your script doesn't take an unsanitized path from user input and use it to load the PDF file.
Your script should ideally take a parameter such as ?file_id=123. The script would first check the user has a valid session, and then the file_id would be used to look up the PDF file and stream it to the user. Here's a description of how to stream a file using PHP: https://stackoverflow.com/a/16847068/366965
Here is one way it can be done in PHP:
<?php
if (!empty($_GET) && !isset($_GET['pdf'])) {
echo "No PDF";
} else {
$pdfId = $_GET['pdf'];
// Here you should retrieve the filename by pdfId
//$pdfFilePath = sprintf("../pdf/%s.pdf", $retrievedFileName);
$pdfFilePath = "../pdf/dummy.pdf";
if (file_exists($pdfFilePath)) {
header('Content-Description: File Transfer');
header("Content-type: application/octet-stream");
header("Content-disposition: attachment;filename=" . basename($pdfFilePath));
header('Content-Length: ' . filesize($pdfFilePath));
readfile($pdfFilePath);
exit;
} else {
echo "File did not exist";
}
}
An extension to this is to use mod_rewrite (or similar) to redirect requests to .pdf to your script - see https://stackoverflow.com/a/10453158/366965 for details.
This is one way it can be done in an .htaccess file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
# only if not an actual file exist
RewriteCond %{REQUEST_FILENAME} !-f
# only if not an actual directory exist
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+)\.pdf index.php?pdf=$1 [NC,L]
</IfModule>
I added a demo project which achieves what you want: https://github.com/joe-niland/docker-apache-php-pdf