php簡單實現sql防注入的方法_php技巧

來源:互聯網
上載者:User

本文執行個體講述了php簡單實現sql防注入的方法。分享給大家供大家參考,具體如下:

這裡沒有太多的過濾,主要是針對php和mysql的組合。

一般性的防注入,只要使用php的 addslashes 函數就可以了。

以下是一段copy來的代碼:

PHP代碼:

$_POST = sql_injection($_POST);$_GET = sql_injection($_GET);function sql_injection($content){if (!get_magic_quotes_gpc()) {if (is_array($content)) {foreach ($content as $key=>$value) {$content[$key] = addslashes($value);}} else {addslashes($content);}}return $content;}

做系統的話,可以用下面的代碼,也是copy來的。

PHP代碼:

function inject_check($sql_str) { return eregi('select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile', $sql_str);  // 進行過濾}function verify_id($id=null) { if (!$id) { exit('沒有提交參數!'); }  // 是否為空白判斷 elseif (inject_check($id)) { exit('提交的參數非法!'); }  // 注射判斷 elseif (!is_numeric($id)) { exit('提交的參數非法!'); }  // 數字判斷 $id = intval($id);  // 整型化 return $id;}function str_check( $str ) { if (!get_magic_quotes_gpc()) {  // 判斷magic_quotes_gpc是否開啟  $str = addslashes($str);  // 進行過濾 } $str = str_replace("_", "\_", $str);  // 把 '_'過濾掉 $str = str_replace("%", "\%", $str);  // 把 '%'過濾掉 return $str;}function post_check($post) { if (!get_magic_quotes_gpc()) {  // 判斷magic_quotes_gpc是否為開啟  $post = addslashes($post);  // 進行magic_quotes_gpc沒有開啟的情況對提交資料的過濾 } $post = str_replace("_", "\_", $post);  // 把 '_'過濾掉 $post = str_replace("%", "\%", $post);  // 把 '%'過濾掉 $post = nl2br($post);  // 斷行符號轉換 $post = htmlspecialchars($post);  // html標記轉換 return $post;}

更多關於PHP相關內容感興趣的讀者可查看本站專題:《php程式設計安全教程》、《php安全過濾技巧總結》、《PHP運算與運算子用法總結》、《PHP網路編程技巧總結》、《PHP基本文法入門教程》、《php操作office文檔技巧總結(包括word,excel,access,ppt)》、《php物件導向程式設計入門教程》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家PHP程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.