如何使 php 脚本只回答来自同一服务器的请求?

发布于 2024-08-22 10:57:57 字数 99 浏览 6 评论 0原文

如果请求脚本位于同一域,如何让我的 PHP 脚本仅应答请求?

** 编辑: PHP文件正在通过ajax请求访问并且是代理,所以我不希望其他人直接请求它出现,这可能吗?

How can I get my PHP script to only answer requests if the requesting script is on the same domain?

** Edit:
The PHP file is being accessed by an ajax request and is proxy, so I don't want others directly requesting it to come up, is this possible?

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

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

发布评论

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

评论(3

撩发小公举 2024-08-29 10:57:57

您可以使用 $_SERVER['REMOTE_ADDR'] 来比较请求该页面的用户的 IP。或者您可以简单地使其成为一个命令行脚本,(显然)需要您从命令行运行它。

编辑:

您想阻止人们通过 AJAX 以外的方式使用该脚本吗?不可能,因为 AJAX 本身是由客户端执行的,因此请求从那里开始。并且始终可以单独调用该脚本;你可以让它变得更难,但你无法阻止它。

You could use $_SERVER['REMOTE_ADDR'] to compare the IP of the user requesting the page. Or you could simply make it a command line script that (obviously) requires you to run it from the command line.

edit:

You want to prevent people from using that script other than via AJAX? Impossible, as AJAX itself is executed by the client, as such the request starts there. And it will be always possible to call that script alone; you can make it harder, but you won't be able to prevent it.

忘你却要生生世世 2024-08-29 10:57:57

没有安全的方法可以做到这一点。一些开发人员会天真地使用 HTTP Referrer 标头字段,但任何聪明到滥用 Ajax 接口的人都可以毫无问题地伪造 Referrer。

There is no safe way to do that. Some developers will naively use the HTTP referrer header field, but anyone smart enough to abuse your ajax interface will have no problem forging the referrer.

走过海棠暮 2024-08-29 10:57:57

您不应该向您自己的服务器发送请求。您应该包含该文件并直接执行函数。

You shouldn't be sending requests to your own server. You should include the file and execute the functions directly.

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