php 過濾特殊字元及sql防注入代碼

來源:互聯網
上載者:User

php 過濾特殊字元及sql防注入代碼
<?php
//方法一
//過濾',",sql語名
addslashes();

//方法二,去除所有html標籤

strip_tags();

//方法三過濾可能產生代碼

function php_sava($str)
{
    $farr = array(
        "/s+/",                                                                                         
        "/<(/?)(script|i?frame|style|html|body|title|link|meta|?|%)([^>]*?)>/isU",  
        "/(<[^>]*)on[a-zA-Z]+s*=([^>]*>)/isU",                                     
     
   );
   $tarr = array(
        " ",
        "<\1\2\3>",           //如果要直接清除不安全的標籤,這裡可以留空
        "\1\2",
   );

  $str = preg_replace( $farr,$tarr,$str);
   return $str;
}

//php sql防注入代碼

class sqlin
{

//dowith_sql($value)
function dowith_sql($str)
{
   $str = str_replace("and","",$str);
   $str = str_replace("execute","",$str);
   $str = str_replace("update","",$str);
   $str = str_replace("count","",$str);
   $str = str_replace("chr","",$str);
   $str = str_replace("mid","",$str);
   $str = str_replace("master","",$str);
   $str = str_replace("truncate","",$str);
   $str = str_replace("char","",$str);
   $str = str_replace("declare","",$str);
   $str = str_replace("select","",$str);
   $str = str_replace("create","",$str);
   $str = str_replace("delete","",$str);
   $str = str_replace("insert","",$str);
   $str = str_replace("'","",$str);
   $str = str_replace(""","",$str);
   $str = str_replace(" ","",$str);
   $str = str_replace("or","",$str);
   $str = str_replace("=","",$str);
   $str = str_replace("%20","",$str);
   //echo $str;
   return $str;
}
//aticle()防SQL注入函數//php教程
function sqlin()
{
   foreach ($_GET as $key=>$value)
   {
       $_GET[$key]=$this->dowith_sql($value);
   }
   foreach ($_POST as $key=>$value)
   {
       $_POST[$key]=$this->dowith_sql($value);
   }
}
}

$dbsql=new sqlin();
?>
===================================================================================
使用方式:
將以上代碼複製建立一個sqlin.php的檔案,然後包含在有GET或者POST資料接收的頁面
原理:
將所有的SQL關鍵字替換為空白
本代碼在留言本中不能使用,若要在留言本中使用請替換其中的
.......
$str = str_replace("and","",$str);

$str = str_replace("%20","",$str);
...
的代碼為:
$str = str_replace("and","&#97;nd",$str);
$str = str_replace("execute","&#101;xecute",$str);
$str = str_replace("update","&#117;pdate",$str);
$str = str_replace("count","&#99;ount",$str);
$str = str_replace("chr","&#99;hr",$str);
$str = str_replace("mid","&#109;id",$str);
$str = str_replace("master","&#109;aster",$str);
$str = str_replace("truncate","&#116;runcate",$str);
$str = str_replace("char","&#99;har",$str);
$str = str_replace("declare","&#100;eclare",$str);
$str = str_replace("select","&#115;elect",$str);
$str = str_replace("create","&#99;reate",$str);
$str = str_replace("delete","&#100;elete",$str);
$str = str_replace("insert","&#105;nsert",$str);
$str = str_replace("'","&#39;",$str);
$str = str_replace(""","&#34;",$str);

 

?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.