Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can check the referring website to see if it's your own with <code>.htaccess</code></p> <pre><code>RewriteEngine On RewriteCond %{HTTP_HOST}@@%{HTTP_REFERER} !^([^@]*)@@http?://\1/.* RewriteRule .*\.pdf [NC,F] </code></pre> <p>That will fail with a forbidden directive (403 error). NC is case insensitivity. The server would need to be configured to show something in the event of a 403 error.</p> <p><strong>In PHP</strong> Additionally you can check this sort of thing with a dynamic page that allows the download. Here's an example of how to do this with PHP:</p> <pre><code>&lt;a href='/download.php?f=myfile&amp;fd=mypath'&gt;Download my PDF&lt;/a&gt; </code></pre> <p>We're taking the <code>.pdf</code> off of the name in the link for security reasons. You could do something like <code>base64_encode</code> the name, but this won't stop a knowledgeable attacker from trying to exploit your system. The <code>f</code> variable is the filename (pre-period) and the 'fd' would be the folder (or path).</p> <p>Example dirs could include <code>pdfs</code> or <code>resources/pdf</code>.</p> <p>It can't start or end with a <code>/</code>. We're not allowing periods in paths or filenames so someone can't do something like <code>pdf/../../..</code>.</p> <p>Code for <code>download.php</code></p> <pre><code>&lt;?php if((preg_match('!^[A-Za-z0-9_-]+$!',$_GET['f']))&amp;&amp;(preg_match('!^[^/][/A-Za-z0-9_-]+[^/]$!',$_GET['fd']))){ //we're hard-coding the line so someone can't spoof something like .htaccess $tempPath = $_GET['fd']; $tempFilename = $_SERVER['DOCUMENT_ROOT'].'/'.$tempPath.'/'.$_GET['f'].'.pdf'; //Make sure it's a real file if(is_file($tempFilename)){ $referrer = $_SERVER['HTTP_REFERER']; $serverName = $_SERVER['SERVER_NAME']; //check the referrer if(strpos($referrer, $serverName)){ $new_filename = $_GET['f'].'.pdf'; // We'll be outputting a PDF header('Content-type: application/pdf'); // It will be called downloaded.pdf $hString = 'Content-Disposition: attachment; filename="'.$new_filename.'"'; header($hString); // The PDF source is in original.pdf readfile($tempFilename); } else { //offsite link header('Location: /download_policy.php'); exit(); } } else { //maybe an old file? Give them the homepage header('Location: /'); exit(); } } else { //hacking attempt header('Location: /'); exit(); } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload