php过滤表单提交的html等危险代码_php实例

WBOY
Release: 2016-06-07 17:15:46
Original
729 people have browsed it

PHP过滤提交表单的html代码里可能有被利用引入外部危险内容的代码。例如,有些时候用户提交表单中含有html内容,但这可能造成显示页面布局混乱,需要过滤掉。

方法一:

复制代码 代码如下:

//get post data
 function PostGet($str,$post=0)
 {
  empty($str)?die('para is null'.$str.'!'):'';
 
  if( $post )
  {
   if( get_magic_quotes_gpc() )
   {
    return htmlspecialchars(isset($_POST[$str])?$_POST
[$str]:'');
   }
   else
   {
    return addslashes(htmlspecialchars(isset($_POST[$str])?
$_POST[$str]:''));
   }
  
  }
  else
  {
   if( get_magic_quotes_gpc() )
   {
    return htmlspecialchars(isset($_GET[$str])?$_GET[$str]:'');
   }
   else
   {
    return addslashes(htmlspecialchars(isset($_GET[$str])?
$_GET[$str]:''));
   }
  }
 }

方法二:

复制代码 代码如下:

function uhtml($str)    
{    
    $farr = array(    
        "/\s+/", //过滤多余空白    
         //过滤 <script>等可能引入恶意内容或恶意改变显示布局的代码,如果不需要插入flash等,还可以加入<object>的过滤 <br /> "/<(\/&#63;)(script|i&#63;frame|style|html|body|title|link|meta|\&#63;|\%)([^>]*&#63;)>/isU", <br /> "/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU",//过滤javascript的on事件 <br /> ); <br /> $tarr = array( <br /> " ", <br /> "<\1\2\3>",//如果要直接清除不安全的标签,这里可以留空 <br /> "\1\2", <br /> ); <br /> $str = preg_replace( $farr,$tarr,$str); <br /> return $str; <br /> } <br /> </script>

很实用的方法吧,希望对大家能有所帮助

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template