Home  >  Article  >  Backend Development  >  php过滤表单提交的html等危险代码_php实例

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

WBOY
WBOYOriginal
2016-06-07 17:15:46620browse

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+/", //过滤多余空白    
         //过滤

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

Statement:
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