防止 .php 文件在 jQuery 请求页面之外加载
我有一个使用 jQuery 在另一个页面中加载的页面。我想知道的是是否有可能以某种方式阻止直接访问在另一个页面中加载的页面。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我有一个使用 jQuery 在另一个页面中加载的页面。我想知道的是是否有可能以某种方式阻止直接访问在另一个页面中加载的页面。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
您可以尝试向您的 get/post 或 cookie 添加一些神奇的值。然后在页面开头检查该值,如果缺少,则返回一些错误页面。
但是,这不会阻止某些用户这样做。如果他们调试您的请求(例如使用 Firebug),那么他们可以使用该幻数复制请求。
You can try adding some magic value, to your get/post or cookie. Then check that value at the start of your page, and return some error page if it's missing.
However, that will NOT prevent some user from doing exactly that. if they debug your requests, for example with Firebug, then they can replicate the request with that magic number.
你不能 100% 阻止它——来自 jQuery 的请求是正常的浏览器请求,所以你最多能做的就是阻止外部链接和随意访问(不辞辛劳地完全复制 HTTP 请求的人仍然能够命中页面;没有办法解决这个问题)。
您可以通过检查 HTTP“referer”标头并确保引用页面是您的来停止外部链接。
您还可以通过检查标头“X-Requested-With”来检查特定请求是否来自 jQuery 与浏览器 - jQuery 添加此标头并用值“XMLHttpRequest”填充它;常规浏览器请求不会有该标头。
您还可以使用 cookie、必须包含在请求中的过期令牌等设置验证系统。但是,如果有人决定直接访问该页面并且可以绕过,那么这需要做更多的工作并且(我认为)太过分了。 HTTP 标头过滤,他们还可以找出你的令牌。
You can't block it 100% -- requests from jQuery are normal browser requests, so the most you can do is prevent external linking and casual access (someone who goes to the trouble to completely duplicate the HTTP request will still be able to hit the page; there's no way around that).
You can stop external linking by checking the HTTP "referer" header and making sure the referring page is yours.
You can also check to see if a particular request is coming from jQuery vs. the browser by checking the header "X-Requested-With" -- jQuery adds this header and populates it with the value "XMLHttpRequest"; a regular browser request won't have that header.
You could also set up a verification system with cookies, expiring tokens that must be part of the request, etc.. but that's a lot more work and (I think) overkill -- if someone's determined to access the page directly and can get around the HTTP header filtering, they can also figure out your tokens.
从类似的 Eran Galperin -direct-access-to-a-php-page">讨论
请同时考虑 Jeremy Ruten 的答案:
上面链接的讨论中有更多聪明的想法。
Quoting Eran Galperin from a similar discussion
Please take this answer by Jeremy Ruten also into account:
More clever thoughts in the discussion linked above.