Only allow access to PHP scripts from a form, not directly

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-27 13:20:22

问题


Being a novice with PHP, I may not be taking the correct route with forms but this way works for me, up to a point. Below is an example of my setup/

I have a form at www.foo.com/add.php, which needs an admin to be logged in to the session. The form inserts data into a database. Once it is submitted, the actions is set to action="scripts/add.php" and then that is redirected using a PHP header function to www.foo.com/done.php.

What I want to know is, can you deny access to the script file directly, e.g. if you go to the script file in a web browser it could enter an empty row into the database or possibly cause some other security issues?


回答1:


If the form is submitted using POST method (with attribute method="post" in <form>), you can still execute your script only on POST requests, by adding this at the top:

 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
   exit;
 }



回答2:


There are a few options available to you:

  1. Validate the form post data before inserting into the database
  2. Include a nonce or other generated value that is only present in the form prior to submission

The problem you're trying to solve really sounds like you want to prohibit blank records from being inserted into the database- not necessarily that you want to prevent access to add.php. This is where option #1 comes into play.

In your current add.php, it sounds like there needs to be some input validation. Basically, you'd check the values that are received by the script to make sure they exist. For example, if add.php accepts a first name, as part of a phonebook app, you'd have code similar to the below:

$firstName = '';
if(isset($_GET['firstName']))
    $firstName = $isset($_GET['firstName']);
// ...
if(trim($firstName) == '')
    //do something to handle an error, either set an error flag or die() with an appropriate message

This is a basic example of input validation, PHP has a validation library you may want to become familiar with: http://www.php.net/manual/en/intro.filter.php

With this input validation in place, someone can navigate to add.php and should receive an error. It will also detect if someone submits a blank form as well.

#2 requires that your form receive a unique value when it's generated called a nonce. The nonce is a unique value that's specific to that instance of the form. The subsequent call to add.php will only accept the request if the nonce is valid. An approach might be to store the nonce in the user's session.

Another note outside the scope of the question, since you're inserting data into a database, you should make sure you have proper escaping of inserted data. If you're using MySQL, see here: http://www.php.net/manual/en/mysqli.real-escape-string.php. If using another database engine, you'll want to lookup the specific library to see how to escape the string.




回答3:


Yuu can try this to check whether request send by post or not

if(isset($_POST)){
continue.......
}



回答4:


in order to secure such pages i have applied the code below.

Except request method, it also checks that the request comes only from specific domain.

$live_site_regex = '/http:\/\/(w{3}|w*).?yourdomain.ext/';
if($_POST && preg_match($live_site_regex,$_SERVER['HTTP_REFERER']) == 1){
//everything is ok
}


来源:https://stackoverflow.com/questions/19448925/only-allow-access-to-php-scripts-from-a-form-not-directly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!