以PHP形式保护所有输入



我的表单有几种类型的输入,包括文本、复选框和单选。我想确认一下表格是否安全。我使用了Prestashop函数isGenericname和isCleanHTML来检查文本和注释字段,确保这些字段是有效的。

Prestashop Validate.php

public static function isGenericName($name)
{
return empty($name) || preg_match('/^[^<>={}]*$/u', $name);
}
public static function isCleanHtml($html, $allow_iframe = false)
{
$events = 'onmousedown|onmousemove|onmmouseup|onmouseover|onmouseout|onload|onunload|onfocus|onblur|onchange';
if (preg_match('/<[s]*script/ims', $html) || preg_match('/('.$events.')[s]*=/ims', $html) || preg_match('/.*script:/ims', $html))
return false;
if (!$allow_iframe && preg_match('/<[s]*(i?frame|form|input|embed|object)/ims', $html))
return false;
return true;
}

这就是在表单PHP文件中调用函数的方式。

if (!Validate::isCleanHtml($message))
$this->errors[] = Tools::displayError('Invalid message');
elseif (!Validate::isGenericName($fname))
$this->errors[] = Tools::displayError('Invalid First Name.');

所以我的问题是。对于无效的复选框和单选框等输入,是否可以不生成错误消息?它们无效的唯一原因是,如果有人在发送之前入侵了他的代码。或者有没有更好的方法来剥离和保护输入?

$checkbox = Tools::getValue('checkbox ');
if (!Validate::isGenericName($checkbox ))
$validCheckbox = $checkbox;

我有68个输入,我想确保是安全的。有没有一个好的PHP函数可以剥离并停止任何类型的SQL注入?Prestashop文档指出"getValue()不能保护你的代码免受黑客攻击(SQL注入、XSS缺陷和CRSF漏洞)。你仍然必须自己保护你的数据。"。

为了防止一阶SQL注入,您可以使用带有mysql准备语句的PDO。当你想把它显示在html页面上时,使用

htmlspecialchars(trim($value), ENT_QUOTES, "UTF-8")`

请确保在响应标头中正确设置了适当的字符编码,并使用元标记指示HTML的字符编码。

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

如果您需要将html输出更新回数据库中。使用

htmlspecialchars_decode(trim($value))

这应该会给你一些保护。

最新更新