① mysql進行全文搜索的時候怎麼過濾html標簽
select text from table
where match(text) against('+php100 +論壇')
② 如何在SQL語句中清除HTML標簽
<%
db="data.mdb"
set conn=server.createobject("Adodb.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath(db)
conn.open connstr
set rs=server.createobject("adodb.recordset")
sql="select * from 表"
rs.open sql,conn,1,3
do while rs.eof=false
rs("欄位一")=Html2Ubb(rs("欄位一")) '在這里使用Html2Ubb函數將欄位的數據轉換成純文本形式後寫入資料庫
rs("欄位二")=Html2Ubb(rs("欄位二"))
rs("欄位三")=Html2Ubb(rs("欄位二"))
rs.update
rs.movenext
loop
rs.close
set rs=nothing
conn.close
set conn=nothing
Public Function Html2Ubb(ByVal strContent)
On Error Resume Next
If Len(strContent) > 0 Then
Dim re
Set re = New RegExp
re.IgnoreCase = True
re.Global = True
'--清除script腳本
If CInt(ArrayCodes(0)) = 1 Then
re.Pattern = "(<s+cript(.+?)<\/s+cript>)"
strContent = re.Replace(strContent, "")
End If
'--清除所有iframe框架
If CInt(ArrayCodes(1)) = 1 Then
re.Pattern = "(<iframe(.+?)<\/iframe>)"
strContent = re.Replace(strContent, "")
End If
'--清除所有object對象
If CInt(ArrayCodes(2)) = 1 Then
re.Pattern = "(<object(.+?)<\/object>)"
strContent = re.Replace(strContent, "")
End If
'--清除所有java applet
If CInt(ArrayCodes(3)) = 1 Then
re.Pattern = "(<applet(.+?)<\/applet>)"
strContent = re.Replace(strContent, "")
End If
'--清除所有div標簽
If CInt(ArrayCodes(4)) = 1 Then
re.Pattern = "(<DIV>)|(<DIV(.+?)>)"
strContent = re.Replace(strContent, "")
re.Pattern = "(<\/DIV>)"
strContent = re.Replace(strContent, "")
End If
'--清除所有font標簽
If CInt(ArrayCodes(5)) = 1 Then
re.Pattern = "(<FONT>)|(<FONT(.+?)>)"
strContent = re.Replace(strContent, "")
re.Pattern = "(<\/FONT>)"
strContent = re.Replace(strContent, "")
End If
'--清除所有span標簽
If CInt(ArrayCodes(6)) = 1 Then
re.Pattern = "(<SPAN>)|(<SPAN(.+?)>)"
strContent = re.Replace(strContent, "")
re.Pattern = "(<\/SPAN>)"
strContent = re.Replace(strContent, "")
End If
'--清除所有A標簽
' If CInt(ArrayCodes(7)) = 1 Then
re.Pattern = "(<A>)|(<A(.+?)>)"
strContent = re.Replace(strContent, "")
re.Pattern = "(<\/A>)"
strContent = re.Replace(strContent, "")
' End If
'--清除所有img標簽
If CInt(ArrayCodes(8)) = 1 Then
re.Pattern = "(<IMG(.+?)>)"
strContent = re.Replace(strContent, "")
End If
'--清除所有FORM標簽
If CInt(ArrayCodes(9)) = 1 Then
re.Pattern = "(<FORM>)|(<FORM(.+?)>)"
strContent = re.Replace(strContent, "")
re.Pattern = "(<\/FORM>)"
strContent = re.Replace(strContent, "")
End If
'--清除所有HTML標簽
If CInt(ArrayCodes(10)) = 1 Then
re.Pattern = "<(.[^>]*)>"
strContent = re.Replace(strContent, "")
End If
re.Pattern = "(" & Chr(8) & "|" & Chr(9) & "|" & Chr(10) & "|" & Chr(13) & ")"
strContent = re.Replace(strContent, vbNullString)
re.Pattern = "(<!--(.+?)-->)"
strContent = re.Replace(strContent, vbNullString)
re.Pattern = "(<TBODY>)"
strContent = re.Replace(strContent, "")
re.Pattern = "(<\/TBODY>)"
strContent = re.Replace(strContent, "")
re.Pattern = "(<" & Chr(37) & ")"
strContent = re.Replace(strContent, "<%")
re.Pattern = "(" & Chr(37) & ">)"
strContent = re.Replace(strContent, "%>")
Set re = Nothing
Html2Ubb = strContent
Else
Html2Ubb = ""
End If
Exit Function
End Function
%>
③ 有什麼比較簡單的方法 能防止html標簽注入和資料庫注入
一,HTML防注入。
一般的注入都是在字元串中加入了html標簽,用下JAVA代碼可以去掉這部分代碼。
代碼如下,自己封裝成方法即可。
String msge = "asdasdasdasd <div id=\"f\">asdfsdf";
System.out.println(msge);
msge = msge.replace("&", "&");
msge = msge.replace("<", "<");
msge = msge.replace(" ", " ");
msge = msge.replace(">", ">");
msge = msge.replace("\"", """);
msge = msge.replace("'", "&qpos;");
System.out.println(msge);
二、防SQL注入
最簡單最容易的是限制用戶輸入。
簡單點的就是不允許用戶輸入單引號 和 --,因為單引號號--在SQL中都是影響執行的。
但SQL注入是多方面的,防止的方法也有很多種。
1、地址欄禁止特殊字元防SQL注入
把特殊字元(如and、or、'、")都禁止提交就可以防止注入了。
2、php過濾html字元串,防止SQL注入
批量過濾post,get敏感數據
$_GET = stripslashes_array($_GET);
$_POST = stripslashes_array($_POST);
數據過濾函數
function stripslashes_array(&$array) {
while(list($key,$var) = each($array)) {
if ($key != 'argc' && $key != 'argv' && (strtoupper($key) != $key || ''.intval($key) == "$key")) {
if (is_string($var)) {
$array[$key] = stripslashes($var);
}
if (is_array($var)) {
$array[$key] = stripslashes_array($var);
}
}
}
return $array;
}
3、替換HTML尾標簽
function lib_replace_end_tag($str)
{
if (empty($str)) return false;
$str = htmlspecialchars($str);
$str = str_replace( '/', "", $str);
$str = str_replace("\\", "", $str);
$str = str_replace(">", "", $str);
$str = str_replace("<", "", $str);
$str = str_replace("<SCRIPT>", "", $str);
$str = str_replace("</SCRIPT>", "", $str);
$str = str_replace("<script>", "", $str);
$str = str_replace("</script>", "", $str);
$str=str_replace("select","select",$str);
$str=str_replace("join","join",$str);
$str=str_replace("union","union",$str);
$str=str_replace("where","where",$str);
$str=str_replace("insert","insert",$str);
$str=str_replace("delete","delete",$str);
$str=str_replace("update","update",$str);
$str=str_replace("like","like",$str);
$str=str_replace("drop","drop",$str);
$str=str_replace("create","create",$str);
$str=str_replace("modify","modify",$str);
$str=str_replace("rename","rename",$str);
$str=str_replace("alter","alter",$str);
$str=str_replace("cas","cast",$str);
$str=str_replace("&","&",$str);
$str=str_replace(">",">",$str);
$str=str_replace("<","<",$str);
$str=str_replace(" ",chr(32),$str);
$str=str_replace(" ",chr(9),$str);
$str=str_replace(" ",chr(9),$str);
$str=str_replace("&",chr(34),$str);
$str=str_replace("'",chr(39),$str);
$str=str_replace("<br />",chr(13),$str);
$str=str_replace("''","'",$str);
$str=str_replace("css","'",$str);
$str=str_replace("CSS","'",$str);
return $str;
}
三、專業的事情交給專業的工具去做。
安裝安全軟體。例如,在伺服器中安裝「伺服器安全狗」,可以設置防注入,防攻擊的設置,只要設置好安全規則,就可以屏蔽大多數攻擊入侵。
④ 如何過濾HTML標簽,或者讀取數據時,去處HTML標簽
如果你把html標簽除掉了問題會更大。
如果你不需要所見即所得的編輯器,那麼可以直接使用textarea。在把用戶輸入的html標簽過濾掉就行了。
⑤ 用正則表達式過濾HTML標簽
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* <p>
* Title: HTML相關的正則表達式工具類
* </p>
* <p>
* Description: 包括過濾HTML標記,轉換HTML標記,替換特定HTML標記
* </p>
* <p>
* Copyright: Copyright (c) 2006
* </p>
*
* @ hejian
* @version 1.0
* @createtime 2006-10-16
*/
public class HtmlRegexpUtil {
private final static String regxpForHtml = "<([^>]*)>"; // 過濾所有以<開頭以>結尾的標簽
private final static String regxpForImgTag = "<\\s*img\\s+([^>]*)\\s*>"; // 找出IMG標簽
private final static String regxpForImaTagSrcAttrib = "src=\"([^\"]+)\""; // 找出IMG標簽的SRC屬性
/**
*
*/
public HtmlRegexpUtil() {
// TODO Auto-generated constructor stub
}
/**
*
* 基本功能:替換標記以正常顯示
* <p>
*
* @param input
* @return String
*/
public String replaceTag(String input) {
if (!hasSpecialChars(input)) {
return input;
}
StringBuffer filtered = new StringBuffer(input.length());
char c;
for (int i = 0; i <= input.length() - 1; i++) {
c = input.charAt(i);
switch (c) {
case '<':
filtered.append("<");
break;
case '>':
filtered.append(">");
break;
case '"':
filtered.append(""");
break;
case '&':
filtered.append("&");
break;
default:
filtered.append(c);
}
}
return (filtered.toString());
}
/**
*
* 基本功能:判斷標記是否存在
* <p>
*
* @param input
* @return boolean
*/
public boolean hasSpecialChars(String input) {
boolean flag = false;
if ((input != null) && (input.length() > 0)) {
char c;
for (int i = 0; i <= input.length() - 1; i++) {
c = input.charAt(i);
switch (c) {
case '>':
flag = true;
break;
case '<':
flag = true;
break;
case '"':
flag = true;
break;
case '&':
flag = true;
break;
}
}
}
return flag;
}
/**
*
* 基本功能:過濾所有以"<"開頭以">"結尾的標簽
* <p>
*
* @param str
* @return String
*/
public static String filterHtml(String str) {
Pattern pattern = Pattern.compile(regxpForHtml);
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result1 = matcher.find();
while (result1) {
matcher.appendReplacement(sb, "");
result1 = matcher.find();
}
matcher.appendTail(sb);
return sb.toString();
}
/**
*
* 基本功能:過濾指定標簽
* <p>
*
* @param str
* @param tag
* 指定標簽
* @return String
*/
public static String fiterHtmlTag(String str, String tag) {
String regxp = "<\\s*" + tag + "\\s+([^>]*)\\s*>";
Pattern pattern = Pattern.compile(regxp);
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result1 = matcher.find();
while (result1) {
matcher.appendReplacement(sb, "");
result1 = matcher.find();
}
matcher.appendTail(sb);
return sb.toString();
}
/**
*
* 基本功能:替換指定的標簽
* <p>
*
* @param str
* @param beforeTag
* 要替換的標簽
* @param tagAttrib
* 要替換的標簽屬性值
* @param startTag
* 新標簽開始標記
* @param endTag
* 新標簽結束標記
* @return String
* @如:替換img標簽的src屬性值為[img]屬性值[/img]
*/
public static String replaceHtmlTag(String str, String beforeTag,
String tagAttrib, String startTag, String endTag) {
String regxpForTag = "<\\s*" + beforeTag + "\\s+([^>]*)\\s*>";
String regxpForTagAttrib = tagAttrib + "=\"([^\"]+)\"";
Pattern patternForTag = Pattern.compile(regxpForTag);
Pattern patternForAttrib = Pattern.compile(regxpForTagAttrib);
Matcher matcherForTag = patternForTag.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result = matcherForTag.find();
while (result) {
StringBuffer sbreplace = new StringBuffer();
Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag
.group(1));
if (matcherForAttrib.find()) {
matcherForAttrib.appendReplacement(sbreplace, startTag
+ matcherForAttrib.group(1) + endTag);
}
matcherForTag.appendReplacement(sb, sbreplace.toString());
result = matcherForTag.find();
}
matcherForTag.appendTail(sb);
return sb.toString();
}
}
⑥ 求助 SQL語句 過濾HTML代碼
<%Function htmlDecode(strHTML)
Dim objRegExp, Match, Matches
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
'取閉合的<>
objRegExp.Pattern = "<.+?>"
'進行匹配
Set Matches = objRegExp.Execute(strHTML)
' 遍歷匹配集合,並替換掉匹配的項專目
For Each Match in Matches
strHtml=Replace(strHTML,Match.Value,"")
Next
htmlDecode=strHTML
Set objRegExp = Nothing
End Function
%>
調用屬函數
⑦ sql只讀表中的文字不讀html標簽中的東西例如(<font>asdsad</font>這些內容不讀)
需要自己寫過濾判斷,提示:標簽共有特徵是"<"或">",你可以以文件流的方式讀取整個文件,然後再截取標簽,剩下的就是內容了!
⑧ 過濾html代碼的sql自定義函數
用替代就可以了,跟UBB一個道理
函數舉例:
words=request("words")
words=Replace(words,"<","&?lt;") '問號去掉,因為網路沒有屏蔽,沒辦法發出來
過濾後,寫如資料庫的就是&?lt;(去掉問號),再調用資料庫數據就又還原,所以不能執行html代碼了
另外還有"、'、< 、> 、% 、& 、( 、) 、; 、+、-、[ 、] 、{ 、} 我知道的就這些了。其實編碼用到的關鍵字元大都需要過濾,防止SQL注入漏洞。
至於不知道相應的字元,就拿DW、FP之類的直接Font敏感字元,然後查看代碼就是了
⑨ 怎麼過濾html標簽
過濾html標簽代碼如下:
public string checkStr(string html)
{
System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" on[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
html = regex1.Replace(html, ""); //過濾<script></script>標記
html = regex2.Replace(html, ""); //過濾href=javascript: (<A>) 屬性
html = regex3.Replace(html, " _disibledevent="); //過濾其它控制項的on...事件
html = regex4.Replace(html, ""); //過濾iframe
html = regex5.Replace(html, ""); //過濾frameset
html = regex6.Replace(html, ""); //過濾frameset
html = regex7.Replace(html, ""); //過濾frameset
html = regex8.Replace(html, ""); //過濾frameset
html = regex9.Replace(html, "");
html = html.Replace(" ", "");
html = html.Replace("</strong>", "");
html = html.Replace("<strong>", "");
return html;
}
⑩ 有沒有sql語句可以刪除某個欄位中的某些html代碼
1、SQL語句暫時沒有這樣的處理方法
2、不過可以使用自定義函數+DLL的方式,思路如下版
1)、使用權.net編寫DLL,裡面需要有正則表達式的替換方法
2)、然後在SQL資料庫中注冊這個DLL
3)、編寫一個自定義函數,用於剔除HTML代碼