简体   繁体   English

如何防止 URL 直接访问?

[英]How to Prevent Direct URL Access?

Please help me to resolve hotlinking, how to prevent direct access to this URL and redirect visitors to index.php:请帮我解决盗链问题,如何防止直接访问此 URL 并将访问者重定向到 index.php:

http://www.example.com/index.php?link=http://www.anysite.com/dir/file&name=on&email=on&submit=on

are you searching for something like this:你在寻找这样的东西:

if(!strpos('mysite.com',$_SERVER["HTTP_REFERER"])) header('Location: index.php')

For purposes of answering this, I'm going to assume you don't care if the same user accesses it multiple times (provided that the first visit came through the main index page).为了回答这个问题,我假设您不在乎同一个用户是否多次访问它(假设第一次访问是通过主索引页面进行的)。 This also assumes the user will accept a cookies.这也假设用户将接受 cookies。

When on the main index page:在主索引页面上时:

  1. start up a session on index.php在 index.php 上启动 session
  2. put some random value inside their session.在他们的 session 中放入一些随机值。 eg: md5(microtime()) = af1929191...例如:md5(microtime()) = af1929191...
  3. also put that random value inside each url as another parameter eg: index.php?verify=af19...&link=http://foo.com还将该随机值放入每个 url 作为另一个参数,例如: index.php?verify=af19...&link=http://foo.com

When loading a url:加载 url 时:

  1. check to see if the "verify" param is set if it isn't there, redirect them back to main index page.检查是否设置了“验证”参数(如果不存在),将它们重定向回主索引页面。 Or more helpfully, since you are creating a weird behavior, show them a error message indicating what you are doing, and why.或者更有帮助的是,因为您正在创建一个奇怪的行为,所以向他们显示一条错误消息,说明您在做什么以及为什么。
  2. Start up the session and make sure that the value in their session matches the value in the url.启动 session 并确保其 session 中的值与 url 中的值匹配。

Using an htaccess file is a common solution to this problem:使用 htaccess 文件是解决此问题的常见方法:
from http://altlab.com/htaccess_tutorial.html来自http://altlab.com/htaccess_tutorial.html
This code in particular redirects anyone trying to hotlink an image.此代码特别重定向任何试图热链接图像的人。

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ http://img148.imageshack.us/img148/237/hotlinkp.gif [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM