简体   繁体   English

过滤表单输入

[英]filtering form inputs

I have a simple contact form with name, email, a select list, and textarea. 我有一个简单的联系表单,其中包含姓名,电子邮件,选择列表和文本区域。 In my mailer php script I'm trying to add a simple filter to prevent SQL injection or other forms of hacking. 在我的邮件php脚本中,我试图添加一个简单的过滤器以防止SQL注入或其他形式的黑客入侵。

For example, I'm using 例如,我正在使用

$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_SPECIAL_CHARS);

Is this good? 这个好吗?

Firstly let me tell you that about 85% of protection methods are done with 2 functions. 首先让我告诉您,约有85%的保护方法是通过2个功能完成的。

Firstly if someone sends some data to your site such as $_POST['name'] , and you wish to use this value back on html side such as <p>The following string: {$_POST['name']} is invalid</p> then you should ALWAYS make sure that that value has been through htmlspecialchars , this will protect most of XSS Attempts 首先,如果有人向您的站点发送了一些数据,例如$_POST['name'] ,而您希望在html端使用此值,例如<p>The following string: {$_POST['name']} is invalid</p>那么您应该始终确保该值已通过htmlspecialchars进行保护,这将保护大多数 XSS尝试

Next is injection, if the value of $_POST['name'] is going into your database just make sure that you use mysql_real_escape_string on that value. 接下来是注入,如果$_POST['name']值进入数据库,只需确保对该值使用mysql_real_escape_string

that will give you 100% protection from sql injection, but all that means is your db cannot run commands from the user, that dont mean that the text is what it should be. 这将为您提供100%的sql注入保护,但这仅意味着您的数据库无法运行来自用户的命令,这并不意味着该文本应为该文本。

The functions that you should always use before inserting data into your database are 在将数据插入数据库之前,应始终使用的功能是

This is called Validation and is only needed for yout to make sure the data the user is submitting is what you want such as filter_var would be used to validate that the email they entered is an email and not just some blah blah 这称为验证 ,仅需要您确认用户提交的数据就是您想要的数据,例如filter_var将用于验证他们输入的电子邮件是电子邮件,而不仅仅是一些等等

What i usually tent do do is to run a clean function to make sure that all imputed data is clean with htmlspecialchars 我通常做的事情是运行一个clean函数,以确保使用htmlspecialchars清除所有估算的数据

example: 例:

function clean($array)
{
    foreach($array as $key => $val)
    {
        if(is_array($val))
        {
            $array[$key] = clean($val); //Recursive 
        }else
        {
            $array[$key] = htmlspecialchars($val, ENT_QUOTES);
        }
    }
    return $array;
}

Then do the following to make sure that your safe from XSS: 然后执行以下操作以确保您不受XSS的侵害:

$_GET = clean($_GET);
$_POST = clean($_POST);

So if someone tried to submit <a href='test'>Test</a> then the value would be converted to &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt 因此,如果有人尝试提交<a href='test'>Test</a>则该值将转换为&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt

FILTER_SANITIZE_SPECIAL_CHARS does HTML-escape '"<>& and characters with ASCII value less than 32. To have an full equivalent for htmlspecialchars() , use FILTER_SANITIZE_FULL_SPECIAL_CHARS which is equivalent to calling htmlspecialchars() with ENT_QUOTES set. Using this function should make the use of mysql_real_escape_string() obsolete, but safety first :) FILTER_SANITIZE_SPECIAL_CHARS HTML转义'“ <>&和ASCII值小于32的字符。要完全等同于htmlspecialchars() ,请使用FILTER_SANITIZE_FULL_SPECIAL_CHARS ,这等效于调用已设置ENT_QUOTES的htmlspecialchars()。 mysql_real_escape_string()的版本已过时,但安全至上:)

see also: http://php.net/manual/en/filter.filters.sanitize.php for more information. 另请参见: http : //php.net/manual/en/filter.filters.sanitize.php以获取更多信息。

To test the effectiveness, try attacking your own site with SQL injection attacks. 为了测试有效性,请尝试使用SQL注入攻击来攻击您自己的站点。 Basically, try passing strings like ' || 1=1 基本上,尝试传递诸如' || 1=1' || 1=1字符串 ' || 1=1 and see if you get an error. ' || 1=1 ,看看是否出现错误。 If you get an error, or if you get an unexpected result, your site is open to attacks. 如果您收到错误消息,或者获得意外结果,则您的网站容易受到攻击。 Otherwise, it is probably working; 否则,它可能会起作用。 but to be sure, make sure you do lots of testing. 但可以肯定的是,请确保您进行了大量测试。

The better option is to use the mysqli extensions and prepared statements. 更好的选择是使用mysqli扩展名和预准备语句。 However, there does exist the mysql_real_escape_string() function which specifically "escapes special characters in a string for use in an SQL statement". 但是,确实存在mysql_real_escape_string()函数,该函数专门“将字符串中的特殊字符转义以用于SQL语句”。

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

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