<p>The job at hand:</p>

<p>I want to make sure that my website's users view a page before they start a download. If they have not looked at the page but try to hotlink to the files directly they should go to the webpage before the download is allowed.</p>

<p>Any suggestions that are better than my idea to send out a cookie and - before the download starts - check if the cookie exists (via .htaccess)?</p>

<p>The webpage and the download files will be located on different servers.</p>

<p>Environment:</p>

<ul>
<li>Apache 2 on all machines</li>
<li>PHP 5 on all machines</li>
<li>MySQL 5 available on the "webpage" server (no access from the download servers)</li>
</ul>

<p>Nathan asked what the problem is that I try to solve, and in fact it is that I want to prevent hotlinks from - for example - forums. If people download from our server, using our bandwidth, I want to show them an page with an ad before the download starts. It doesn't need to be totally secure, but we need to make some money to finance the servers, right? :)</p>

[{'answer_id': 54714, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 1, 'selected': False, 'text': '<p>You could use <a href="http://r.schuil.googlepages.com/phpdsm.html" rel="nofollow noreferrer">distributed server side</a> sessions instead of cookies, but that is probably more trouble than it\'s worth.</p>\n\n<p>You could also <a href="http://rewrite.drbacchus.com/rewritewiki/ImageTheft" rel="nofollow noreferrer">forbid access</a> to requests without a referrer or with a wrong referrer. But that\'s even more fakable than a cookie.</p>\n\n<p>It\'d depend on how much you care.</p>\n'}, {'answer_id': 54724, 'author': 'Brian Warshaw', 'author_id': 1344, 'author_profile': 'https://Stackoverflow.com/users/1344', 'pm_score': 3, 'selected': False, 'text': '<p>Instead of allowing hotlinking to the files, store them outside of a web accessible route. Instead make download requests go to a php script that initiates the download. You can put checks in place to ensure that the requesting page was the one you wanted read.</p>\n'}, {'answer_id': 54801, 'author': 'ceejayoz', 'author_id': 1902010, 'author_profile': 'https://Stackoverflow.com/users/1902010', 'pm_score': 2, 'selected': False, 'text': "<p>An Apache mod_rewrite RewriteRule can do this.</p>\n\n<pre><code>RewriteEngine on\nRewriteCond %{HTTP_REFERER} !^http://www.example.com/page.html$\nRewriteRule file.exe http://www.example.com/page.html [R=301,L]\n</code></pre>\n\n<p>Basically, if a request for <code>file.exe</code> didn't come with <code>page.html</code> as the referrer, it'll 301 redirect the user to <code>page.html</code>.</p>\n"}, {'answer_id': 57698, 'author': 'Gerrit', 'author_id': 5976, 'author_profile': 'https://Stackoverflow.com/users/5976', 'pm_score': 0, 'selected': False, 'text': "<p>I was about to suggest the .htaccess referral trick, but it isn't a very secure method. It's easy to make a PHP script, with a custom http-referral added. If you enter the download page there, it will think you come from that page.</p>\n\n<p>Is this a relevant problem? Can you tell something about the context of your download page?</p>\n"}, {'answer_id': 59069, 'author': 'Nathan', 'author_id': 6062, 'author_profile': 'https://Stackoverflow.com/users/6062', 'pm_score': 1, 'selected': False, 'text': "<p>The solution here depends on the problem you are trying to solve. If you are just trying make sure a direct link doesn't get posted in forums and whatnot, then just checking the referrer with .htaccess should be enough. Of course the referrer can be faked easy enough, so if the risk of someone doing that is a problem, then you'll need to do something else.</p>\n\n<p>A cookie should do the trick if you need something a little more secure. We cannot just use php sessions because the file server and the webserver are on different boxes. But we could create a cookie based on a hash of the time, and some secret value. </p>\n\n<pre><code>cookievalue = sha1('secretvalue'.date('z-H'));\n</code></pre>\n\n<p>When the user requests the actual file, the fileserver generates this cookie again and makes sure it matches the users. This means, even if the user forges the cookie, it will be invalid after an hour, so who cares, and they can't generate their own new one, because they do not know the secret value.</p>\n"}, {'answer_id': 1749061, 'author': 'BlaM', 'author_id': 999, 'author_profile': 'https://Stackoverflow.com/users/999', 'pm_score': 1, 'selected': True, 'text': '<p>I\'m going to use <a href="http://www.synd.info/downloads/releases/" rel="nofollow noreferrer">mod_auth_token</a> to make sure that the user has a "recent link".</p>\n\n<p>With mod_auth_token you can create expiring links, i.e. if someone decides to take an existing link and post it on another website, this link will expire after a specified time. That will result in a 403 FORBIDDEN which I can catch and redirect the user to the HTML page I want him on.</p>\n'}]
['https://Stackoverflow.com/questions/54669', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/999/']