简体   繁体   English

阻止直接访问jquery的发布网址

[英]prevent direct access to jquery post url

i've a jquery script which post/get data to .php script. 我有一个jQuery脚本,该脚本将数据post/get到.php脚本。 but i wanna prevent direct access to the php script. 但我想防止直接访问php脚本。 for example if the user look at the html source code,they will be able to access the php script directly by copying the url from the js file and i dont want that. 例如,如果用户查看html源代码,他们将能够通过从js文件复制url直接访问php脚本,而我不希望那样。 how do i prevent users from doing that?? 我如何防止用户这样做? i want the user to use it via the html UI. 我希望用户通过html UI使用它。 i've google but found no link on this. 我有谷歌,但没有找到任何链接。 however, i did notice that some popular websites are able to do that. 但是,我确实注意到一些受欢迎的网站能够做到这一点。 how should i go about doing this?? 我应该怎么做呢?

It seems like a simple redirect is what you're looking for here. 您似乎在这里寻找一个简单的重定向。

Add something like this to the top of your php file. 在您的php文件顶部添加类似的内容。 This will prevent the page from being accessed if the proper post has not been made. 如果没有进行适当的张贴,这将阻止访问该页面。 Of course you'll have to change the post and redirect to content more relevant to your project. 当然,您必须更改帖子并重定向到与您的项目更相关的内容。

if (!isset($_POST['data'])) {
    header('Location: your-redirect-location');
}

You may also be able to redirect based on the $_SERVER['HTTP_REFERER'] variable. 您也可以基于$ _SERVER ['HTTP_REFERER']变量进行重定向。

EDIT: I was going to explain this in a comment but it's too long. 编辑:我打算在评论中对此进行解释,但时间太长。 I should note that this is a simple solution. 我应该注意,这是一个简单的解决方案。 It will keep people from accidentally accessing your script. 这将使人们避免意外访问您的脚本。 It's really difficult to create a 100% secure solution for your issue, and if somebody really wants to access it, they will be able to. 为您的问题创建100%安全的解决方案确实很困难,而且如果有人真的想访问它,他们将能够提供帮助。 If you don't have anything secure in the script in question, this will be fine. 如果您在相关脚本中没有任何安全措施,那就可以了。 Otherwise, you'll have to look for an alternative. 否则,您将不得不寻找替代方案。

Here is one solution: 这是一种解决方案:

<?php
if(isset($_POST["post_var]))
{
//to the code you want to do when the post is made
}
else
{
//do what you want to do when the user views the post page
}
?>

how do i prevent users from doing that? 我如何防止用户这样做?

You can't - all you can do is mitigate the risk people can fiddle with your script. 您不能-您所能做的就是减轻人们摆弄脚本的风险。 Making sure you have the right HTTP_REFERER and/or POST data are both useful in that regard: a "malicious" user would need more than pointing her browser to the URL. 在这方面,确保您拥有正确的HTTP_REFERER和/或POST数据都非常有用:“恶意”用户需要的不仅仅是将浏览器指向URL。

More techniques can be used here: 可以在此处使用更多技术:

  • using session variables: you might not want users that are not logged in - if applicable - to use the URL. 使用会话变量:您可能不希望未登录的用户(如果适用)使用URL。
  • using a one-time challenge (token): you can place a value in the HTML page and have the JS code send this value along with the POST request. 使用一次性挑战(令牌):您可以在HTML页面中放置一个值,并让JS代码将此值与POST请求一起发送。 You store this value in the session when it is generated. 将该值存储在会话中时将其存储。 Checking the POSTed token against the session token guarantees the user has at least "seen" the HTML page before submitting data - this can also be useful to prevent duplicate submissions. 根据会话令牌检查POSTed令牌,以确保用户在提交数据之前至少“看到”了HTML页面-这对于防止重复提交也很有用。

However, remember that anything a browser can do, people can do it as well. 但是,请记住,浏览器可以执行的任何操作,人们也可以执行。 All these techniques can prevent the curious from doing harm, but not the malicious. 所有这些技术都可以阻止好奇者造成伤害,但不能阻止恶意行为。

All you can do is making sure nobody can really harm you, and in this regard, your Ajax URL is no different than any other URL of your site: if it's publicly reachable, it has to be secured using whatever technique you already use elsewhere - sessions, user rights, etc. 您所要做的就是确保没有人能真正伤害到您,就此而言,您的Ajax URL与您网站的其他URL相同:如果可以公开访问,则必须使用您已经在其他地方使用的任何技术来保护它-会话,用户权限等。

After all, why should you care that users use this URL not using a browser ? 毕竟,您为什么要关心用户不使用浏览器使用此URL? You might want to think of it in terms of an API call that, incidentally, your page happens to use. 您可能想用一个偶然发生的您页面使用的API调用来考虑它。

Your problem is similar to and has the same problems as a cross site request forgery . 您的问题类似于跨站点请求伪造 ,并且具有相同的问题。

To reduce your risk, you can check the request method, check the referrer, and check the origin if set. 为了降低风险,您可以检查请求方法,检查引荐来源并检查来源(如果已设置)。 The best way is to have a secret token that was generated on the server that the client transmits back in every request. 最好的方法是拥有在服务器上生成的秘密令牌,客户端在每个请求中都将其返回。 Since you're dealing with friendly users who have access to your live code, they may be able to debug the script and find the value, but it would only be for one session and would be a real hassle. 由于您正在与有权访问您的实时代码的友好用户打交道,因此他们可能能够调试脚本并找到值,但这仅适用于一次会话,这确实是一件麻烦事。

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

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