简体   繁体   English

禁止直接访问PHP文件

[英]Disallowing PHP file to be accessed directly

I have an html form which when it gets submitted it calls a JavaScript function which by using Ajax gets information from a PHP file using post and displays it back to the page. 我有一个html表单,该表单在提交时会调用JavaScript函数,该函数使用Ajax使用post从PHP文件中获取信息,并将其显示回页面。

The question is, is it possible to make the PHP file only accessible when using the above method instead of users being able to access it directly if they go through the JS method and find it's location? 问题是,是否可以使PHP文件仅在使用上述方法时可访问,而不是如果用户通过JS方法并找到其位置,则用户无法直接访问它?

Edit: Adding a bit more information to help people out. 编辑:添加更多的信息来帮助人们。 Ajax is calling an external php file in order to update the contents of the website based on what the php file returns. Ajax正在调用一个外部php文件,以便根据php文件返回的内容来更新网站的内容。 Since all the Ajax calls are made in the JavaScript someone can easily find out the location and the arguments the function is using and basically call the php file directly, which is what I'm trying to avoid. 由于所有Ajax调用都是在JavaScript中进行的,因此有人可以轻松找出该函数使用的位置和参数,并基本上直接调用php文件,这就是我要避免的事情。

Using PHP sessions is a bit hard in this case, since I'm using Ajax I can't destroy the session once the external PHP file is done since if I do the session never renews because I'm using Ajax to update the content of the website without refreshing it. 在这种情况下,使用PHP会话会有点困难,因为我使用的是Ajax,所以一旦外部PHP文件完成,我就无法销毁该会话,因为如果我这样做,则该会话永远不会更新,因为我正在使用Ajax更新内容。该网站而无需刷新。

I agree with limscoder. 我同意limscoder。

Here is how I use tokens on submitting forms. 这是我在提交表单时使用令牌的方式。

1) initialize it and save it on server (when the page is loaded on the client side) 1)初始化它并将其保存在服务器上(当页面在客户端加载时)

$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));

2) add it to the form 2)将其添加到表单

<input type="hidden" name="token" value="<?php echo $token;?>" />

3) when submitted I check the token using 3)提交时我使用

if(isset($_SESSION['token'], $_POST['token']) && $_SESSION['token'] == $_POST['token'])
{
//code goes here
}

It sounds like you're describing a type of cross site request forgery . 听起来您正在描述一种跨站点请求伪造 The normal way of preventing this is by including a server generated token as a form value, and then validating it against a value stored in the user's session when the form is submitted. 防止这种情况的正常方法是,将服务器生成的令牌包括为表单值,然后在提交表单时针对存储在用户会话中的值对其进行验证。 Check out the link for instructions on how to properly generate and validate the token. 请查看链接,以获取有关如何正确生成和验证令牌的说明。

You can test what page called your script by checking the HTTP_REFERER (http://php.net/manual/en/reserved.variables.server.php) 您可以通过检查HTTP_REFERER(http://php.net/manual/en/reserved.variables.server.php)测试哪个页面调用了脚本。

BUT...it can be spoofed, so will only discourage casual attempts at getting to your data 但是...它可以被欺骗,因此只会阻止随意尝试获取数据

Is the html page secured? HTML页面是否安全? If anyone can just access the html page to call your ajax page, why bother to spend time trying to secure it. 如果任何人都可以访问html页面来调用您的ajax页面,为什么还要花时间尝试保护它。 :) :)

However if you really need to do so, you might need to convert the html page to a php page so that you can generate a token to be placed in the form. 但是,如果确实需要这样做,则可能需要将html页面转换为php页面,以便可以生成要放置在表单中的令牌。 A good token would be the sha of a secret string appended with the year, month, day and hour. 一个好的标记是带有年,月,日和小时的秘密字符串的阴影。 When the token is sent back, use the same way to generate a test token and compare the two. 当令牌被发回时,使用相同的方式生成测试令牌并比较两者。 This makes the token valid only for 1 hour. 这使得令牌仅在1小时内有效。 Of cause, in the ajax, you might need to test against last hour's token, in case the user got the form at the last minute of the hour and submits after the hour. 当然,在ajax中,您可能需要根据上一小时的令牌进行测试,以防用户在该小时的最后一分钟收到表单并在该小时之后提交。

Edit--- If your main concern are bots then just use a captcha test. 编辑---如果您主要关注的是机器人,则只需使用验证码测试即可。

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

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