防止 .php 文件在 jQuery 请求页面之外加载

发布于 2024-08-07 03:31:57 字数 69 浏览 3 评论 0 原文

我有一个使用 jQuery 在另一个页面中加载的页面。我想知道的是是否有可能以某种方式阻止直接访问在另一个页面中加载的页面。

I have a page that is loaded within another page using jQuery. What I wanted to know is if it is possible to somehow block direct access to that page that gets loaded within another page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

聚集的泪 2024-08-14 03:31:57

您可以尝试向您的 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.

月亮坠入山谷 2024-08-14 03:31:57

你不能 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.

过潦 2024-08-14 03:31:57

从类似的 Eran Galperin -direct-access-to-a-php-page">讨论

正如其他人所说,Ajax请求可以
被模仿并创造适当的
标头。如果你想有一个基本的
检查请求是否是 Ajax
您可以使用的请求:

if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
 //请求标识为ajax请求
}

但是你永远不应该将你的
这张支票的安全性。它将
消除对页面的直接访问
如果这就是您所需要的。

请同时考虑 Jeremy Ruten 的答案:

没有办法保证
他们通过 AJAX 访问它。
直接访问和AJAX访问均可
来自客户端,因此可以轻松地
被伪造。

你为什么要这样做?

如果是因为 PHP 代码不是
非常安全,让PHP代码更安全
安全的。 (例如,如果您的 AJAX
将用户 ID 传递给 PHP 文件,
在PHP文件中编写代码以使得
确保这是正确的用户 ID。)

上面链接的讨论中有更多聪明的想法。

Quoting Eran Galperin from a similar discussion

As others have said, Ajax request can
be emulated be creating the proper
headers. If you want to have a basic
check to see if the request is an Ajax
request you can use:

if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
 //Request identified as ajax request
}

However you should never base your
security on this check. It will
eliminate direct accesses to the page
if that is what you need.

Please take this answer by Jeremy Ruten also into account:

There is no way of guaranteeing that
they're accessing it through AJAX.
Both direct access and AJAX access
come from the client, so it can easily
be faked.

Why do you want to do this anyways?

If it's because the PHP code isn't
very secure, make the PHP code more
secure. (For example, if your AJAX
passes the user id to the PHP file,
write code in the PHP file to make
sure that is the correct user id.)

More clever thoughts in the discussion linked above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文