欧美国产另类久热|951超碰伊人久久|伊人影视久久久久|色婷婷亚洲小电影|色东京热69XX|婷婷玖玖深爱网|加勒比东京热网站|无码A级毛片在线观看|一级a毛一级a看|中文字幕人妻欧美

php簡單實現(xiàn)sql防注入的方法
來源:易賢網(wǎng) 閱讀:1333 次 日期:2016-08-26 14:50:03
溫馨提示:易賢網(wǎng)小編為您整理了“php簡單實現(xiàn)sql防注入的方法”,方便廣大網(wǎng)友查閱!

本文實例講述了php簡單實現(xiàn)sql防注入的方法。分享給大家供大家參考,具體如下:

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

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

以下是一段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;

}

做系統(tǒng)的話,可以用下面的代碼,也是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('沒有提交參數(shù)!'); }  // 是否為空判斷

 elseif (inject_check($id)) { exit('提交的參數(shù)非法!'); }  // 注射判斷

 elseif (!is_numeric($id)) { exit('提交的參數(shù)非法!'); }  // 數(shù)字判斷

 $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沒有打開的情況對提交數(shù)據(jù)的過濾

 }

 $post = str_replace("_", "\_", $post);  // 把 '_'過濾掉

 $post = str_replace("%", "\%", $post);  // 把 '%'過濾掉

 $post = nl2br($post);  // 回車轉(zhuǎn)換

 $post = htmlspecialchars($post);  // html標記轉(zhuǎn)換

 return $post;

}

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

更多信息請查看網(wǎng)絡編程
易賢網(wǎng)手機網(wǎng)站地址:php簡單實現(xiàn)sql防注入的方法
關于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點

版權所有:易賢網(wǎng)