Home > php教程 > PHP源码 > php过滤html字符串,防止SQL注入

php过滤html字符串,防止SQL注入

PHP中文网
Release: 2016-05-25 17:14:16
Original
1195 people have browsed it

php过滤html字符串,防止SQL注入

php过滤html字符串,防止SQL注入,用函数把将要写入到数据库的字符串处理下,过滤非法信息,以及恶意的html代码!

//php 批量过滤post,get敏感数据
	if (get_magic_quotes_gpc()) {
	$_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;
	}
          //--------------------------

	// 替换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("&gt", "", $str);
	$str = str_replace("&lt", "", $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("&#39;",chr(39),$str);
	$str=str_replace("<br />",chr(13),$str);
	$str=str_replace("&#39;&#39;","&#39;",$str);
	$str=str_replace("css","&#39;",$str);
	$str=str_replace("CSS","&#39;",$str);
	
	return $str;
	
	}
Copy after login

2. [代码][PHP]代码

引用是直接这样: $xxx = htmlspecialchars($_POST[&#39;xxx&#39;]); 
或者 $xxx = htmlspecialchars($_GET[&#39;xxx&#39;]);
Copy after login

 以上就是php过滤html字符串,防止SQL注入的内容,更多相关内容请关注PHP中文网(www.php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template