PHP防SQL注入攻击
生活随笔
收集整理的這篇文章主要介紹了
PHP防SQL注入攻击
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一般性的防注入,只要使用php的 addslashes 函數就可以了。 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;?? }? 做系統的話,可以用下面的代碼 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防SQL注入攻击的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: homebrew install php
- 下一篇: android 仿人人网滑动侧边栏