Php 我需要做些什么才能将textarea中的数据转换成santize,并将其送入mysql数据库?

Php 我需要做些什么才能将textarea中的数据转换成santize,并将其送入mysql数据库?,php,mysql,html,Php,Mysql,Html,好吧,题目是我的问题。在使用php进入mysql数据库之前,特别是在数据包含html标记的情况下,有人能给我一个清理数据的清单吗?使用mysql\u real\u escape\u string() 使用mysql_real_escape_string() 关闭register\u globals和magic\u quotes,在将用户的任何字符串放入查询之前对其使用。关闭register\u globals和magic\u quotes,在将用户的任何字符串放入查询之前对其使用。您可以使用my

好吧,题目是我的问题。在使用php进入mysql数据库之前,特别是在数据包含html标记的情况下,有人能给我一个清理数据的清单吗?

使用mysql\u real\u escape\u string()


使用mysql_real_escape_string()


关闭
register\u globals
magic\u quotes
,在将用户的任何字符串放入查询之前对其使用。

关闭
register\u globals
magic\u quotes,在将用户的任何字符串放入查询之前对其使用。

您可以使用mysql\u real\u escape\u string,您还可以将htmlentities与addslashes一起使用。。。或者您也可以同时使用这三个…

您可以使用mysql\u real\u escape\u string,您还可以使用带有addslashes的htmlentities。。。或者你也可以同时使用这三个…

当然,mysql\u real\u escape\u string
当处理任何类型的输入时,从开始,我不允许任何立场和白名单,只允许被认为是可以接受的

当然,mysql\u real\u escape\u字符串
当处理任何类型的输入时,从开始,我不允许任何立场和白名单,只允许被认为是可以接受的

插入时,您需要确保数据是MySQL转义的。为此,请使用

在显示数据之前,您需要去除不安全的HTML和/或JavaScript代码。许多人选择将经过消毒的版本存储在数据库中。其他人更喜欢在呈现之前从字符串中去掉难看的HTML

您可以通过一些过滤在PHP中实现这一点。Drupal函数就是一个例子:

function filter_xss($string, $allowed_tags = array('a', 'em', 'strong', 'cite', 'code', 'ul', 'ol', 'li', 'dl', 'dt', 'dd')) {
  // Only operate on valid UTF-8 strings. This is necessary to prevent cross
  // site scripting issues on Internet Explorer 6.
  if (!drupal_validate_utf8($string)) {
    return '';
  }
  // Store the input format
  _filter_xss_split($allowed_tags, TRUE);
  // Remove NUL characters (ignored by some browsers)
  $string = str_replace(chr(0), '', $string);
  // Remove Netscape 4 JS entities
  $string = preg_replace('%&\s*\{[^}]*(\}\s*;?|$)%', '', $string);

  // Defuse all HTML entities
  $string = str_replace('&', '&', $string);
  // Change back only well-formed entities in our whitelist
  // Decimal numeric entities
  $string = preg_replace('/&#([0-9]+;)/', '&#\1', $string);
  // Hexadecimal numeric entities
  $string = preg_replace('/&#[Xx]0*((?:[0-9A-Fa-f]{2})+;)/', '&#x\1', $string);
  // Named entities
  $string = preg_replace('/&([A-Za-z][A-Za-z0-9]*;)/', '&\1', $string);

  return preg_replace_callback('%
    (
    <(?=[^a-zA-Z!/])  # a lone <
    |                 # or
    <!--.*?-->        # a comment
    |                 # or
    <[^>]*(>|$)       # a string that starts with a <, up until the > or the end of the string
    |                 # or
    >                 # just a >
    )%x', '_filter_xss_split', $string);
}
function filter_xss($string,$allowed_tags=array('a','em','strong','cite','code','ul','ol','li','dl','dt','dd')){
//仅对有效的UTF-8字符串进行操作。这是防止交叉的必要条件
//Internet Explorer 6上的站点脚本问题。
如果(!drupal\u validate\u utf8($string)){
返回“”;
}
//存储输入格式
_筛选器_xss_split($allowed_标记,TRUE);
//删除NUL字符(被某些浏览器忽略)
$string=str_replace(chr(0),“”,$string);
//删除Netscape 4 JS实体
$string=preg\u replace('%&\s*\{[^}]*(\}\s*;?\$)%','','',$string);
//解除所有HTML实体的权限
$string=str_replace('&','&;',$string);
//仅更改回白名单中格式良好的实体
//十进制数字实体
$string=preg#u replace('/&;#([0-9]+)/','&#\1',$string);
//十六进制数字实体
$string=preg_replace('/&;#[Xx]0*((?:[0-9A-Fa-f]{2})+)/','&#x\1',$string);
//命名实体
$string=preg_replace('/&;([A-Za-z][A-Za-z0-9]*;)/','&\1',$string);
返回preg_replace_回调('%
(
#评论
|#或
|$)#以a开头或结尾的字符串
|#或
>#只是一个>
)%x','_filter_xss_split',$string);
}

在插入时,您需要确保数据是MySQL转义的。为此,请使用

在显示数据之前,您需要去除不安全的HTML和/或JavaScript代码。许多人选择将经过消毒的版本存储在数据库中。其他人更喜欢在呈现之前从字符串中去掉难看的HTML

您可以通过一些过滤在PHP中实现这一点。Drupal函数就是一个例子:

function filter_xss($string, $allowed_tags = array('a', 'em', 'strong', 'cite', 'code', 'ul', 'ol', 'li', 'dl', 'dt', 'dd')) {
  // Only operate on valid UTF-8 strings. This is necessary to prevent cross
  // site scripting issues on Internet Explorer 6.
  if (!drupal_validate_utf8($string)) {
    return '';
  }
  // Store the input format
  _filter_xss_split($allowed_tags, TRUE);
  // Remove NUL characters (ignored by some browsers)
  $string = str_replace(chr(0), '', $string);
  // Remove Netscape 4 JS entities
  $string = preg_replace('%&\s*\{[^}]*(\}\s*;?|$)%', '', $string);

  // Defuse all HTML entities
  $string = str_replace('&', '&amp;', $string);
  // Change back only well-formed entities in our whitelist
  // Decimal numeric entities
  $string = preg_replace('/&amp;#([0-9]+;)/', '&#\1', $string);
  // Hexadecimal numeric entities
  $string = preg_replace('/&amp;#[Xx]0*((?:[0-9A-Fa-f]{2})+;)/', '&#x\1', $string);
  // Named entities
  $string = preg_replace('/&amp;([A-Za-z][A-Za-z0-9]*;)/', '&\1', $string);

  return preg_replace_callback('%
    (
    <(?=[^a-zA-Z!/])  # a lone <
    |                 # or
    <!--.*?-->        # a comment
    |                 # or
    <[^>]*(>|$)       # a string that starts with a <, up until the > or the end of the string
    |                 # or
    >                 # just a >
    )%x', '_filter_xss_split', $string);
}
function filter_xss($string,$allowed_tags=array('a','em','strong','cite','code','ul','ol','li','dl','dt','dd')){
//仅对有效的UTF-8字符串进行操作。这是防止交叉的必要条件
//Internet Explorer 6上的站点脚本问题。
如果(!drupal\u validate\u utf8($string)){
返回“”;
}
//存储输入格式
_筛选器_xss_split($allowed_标记,TRUE);
//删除NUL字符(被某些浏览器忽略)
$string=str_replace(chr(0),“”,$string);
//删除Netscape 4 JS实体
$string=preg\u replace('%&\s*\{[^}]*(\}\s*;?\$)%','','',$string);
//解除所有HTML实体的权限
$string=str_replace('&','&;',$string);
//仅更改回白名单中格式良好的实体
//十进制数字实体
$string=preg#u replace('/&;#([0-9]+)/','&#\1',$string);
//十六进制数字实体
$string=preg_replace('/&;#[Xx]0*((?:[0-9A-Fa-f]{2})+)/','&#x\1',$string);
//命名实体
$string=preg_replace('/&;([A-Za-z][A-Za-z0-9]*;)/','&\1',$string);
返回preg_replace_回调('%
(
#评论
|#或
|$)#以a开头或结尾的字符串
|#或
>#只是一个>
)%x','_filter_xss_split',$string);
}

这取决于很多事情。如果你不想接受任何HTML,那就更容易了,首先通过
strip\u tags()
来删除所有HTML。之后就安全多了。如果您确实想要接受一些HTML,您可以使用相同的函数有选择地保留其中的一些标记,只需添加要保留的标记即可。例如:
strip_标签($string_to_sanitize,);//仅保留和标签

至于插入数据库,最好在插入数据库之前对任何内容进行清理;采取“不相信任何人”的心态会帮你省去很多麻烦。防止SQL注入非常简单,这是我使用的方法:

$q = sprintf("INSERT INTO table_name (string_field, int_field) VALUES ('%s', %d);",
             mysql_real_escape_string($values['string']),
             mysql_real_escape_string($values['number']));

$result = mysql_query($q, $connection)
一般来说,一旦你打开了允许HTML进入的大门,你就会有很多事情要担心(有一些关于防御XSS的文章)。如果要测试XSS漏洞,请在上尝试这些示例。有一些在那里,你可能永远不会考虑,所以给它看! 此外,如果您接受特定类型的输入(例如:数字、电子邮件、布尔值),请尝试使用PHP中内置的
filter\u var()
函数。它们有一系列内置类型来根据()验证数据,还有一些