前幾天網(wǎng)站給人注入了,現(xiàn)在我給大家來介紹php防止sql注入的幾個(gè)自帶的處理函數(shù),例如PHP的MySQL操作函數(shù)中有addslashes()、mysql_real_escape_string()、mysql_escape_string()等函數(shù)
具體用法:addslashes防止SQL注入
雖然國內(nèi)很多PHP程序員仍在依靠addslashes防止SQL注入,還是建議大家加強(qiáng)中文防止SQL注入的檢查。addslashes的問題在 于黑客可以用0xbf27來代替單引號,而addslashes只是將0xbf27修改為0xbf5c27,成為一個(gè)有效的多字節(jié)字符,其中的0xbf5c仍會(huì) 被看作是單引號,所以addslashes無法成功攔截.
當(dāng)然addslashes也不是毫無用處,它是用于單字節(jié)字符串的處理,多字節(jié)字符還是用mysql_real_escape_string吧.
另外對于php手冊中g(shù)et_magic_quotes_gpc的舉例,代碼如下:
- <?php
- function post_check($post)
- {
- if (!get_magic_quotes_gpc()) // 判斷magic_quotes_gpc是否為打開
- {
- $post = addslashes($post); // 進(jìn)行magic_quotes_gpc沒有打開的情況對提交數(shù)據(jù)的過濾
- }
- $post = str_replace("_", "_", $post); // 把 '_'過濾掉
- $post = str_replace("%", "%", $post); // 把' % '過濾掉
- $post = nl2br($post); // 回車轉(zhuǎn)換
- $post= htmlspecialchars($post); // html標(biāo)記轉(zhuǎn)換
- return $post;
- }//開源代碼phpfensi.com
- ?>
- //或
- <?php
- function inject_check($sql_str)
- {
- return eregi('select|insert|update|delete|'|
- 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;
- }
- ?>
string mysql_real_escape_string ( string $unescaped_string [,resource $link_identifier ] )
本函數(shù)將 unescaped_string 中的特殊字符轉(zhuǎn)義,并計(jì)及連接的當(dāng)前字符集,因此可以安全用于 mysql_query().
Note:mysql_real_escape_string() 并不轉(zhuǎn)義 % 和 _.
mysql_real_escape_string,Example#1 mysql_real_escape_string() 例子,代碼如下:
- <?php
- $item = "Zak's and Derick's Laptop" ;
- $escaped_item = mysql_real_escape_string ( $item );
- printf ( "Escaped string: %sn" , $escaped_item );
- ?>
- //以上例子將產(chǎn)生如下輸出:
- //Escaped string: Zak's and Derick's Laptop
mysql_escape_string
本函數(shù)將 unescaped_string 轉(zhuǎn)義,使之可以安全用于 mysql_query().
注:mysql_escape_string() 并不轉(zhuǎn)義 % 和 _,本函數(shù)和 mysql_real_escape_string() 完全一樣,除了 mysql_real_escape_string() 接受的是一個(gè)連接句柄并根據(jù)當(dāng)前字符集轉(zhuǎn)移字符串之外。mysql_escape_string() 并不接受連接參數(shù),也不管當(dāng)前字符集設(shè)定.
例子 1. mysql_escape_string() 例子,代碼如下:
- <?php
- $item = "Zak's Laptop";
- $escaped_item = mysql_escape_string($item);
- printf ("Escaped string: %sn", $escaped_item);
- ?>
- //輸出:
- //Escaped string: Zak's Laptop
mysql_real_escape_string和mysql_escape_string這2個(gè)函數(shù)的區(qū)別:
mysql_real_escape_string 必須在(PHP 4 >= 4.3.0, PHP 5)的情況下才能使用,否則只能用 mysql_escape_string,兩者的區(qū)別是:mysql_real_escape_string 考慮到連接的當(dāng)前字符集,而mysql_escape_string 不考慮.
我們可以利用判斷來綜合處理,代碼如下:
- function cleanuserinput($dirty){
- if (get_magic_quotes_gpc()) {
- $clean = mysql_real_escape_string(stripslashes($dirty));
- }else{
- $clean = mysql_real_escape_string($dirty);
- }
- return $clean;
- }
總結(jié)一下:* addslashes() 是強(qiáng)行加;* mysql_real_escape_string() 會(huì)判斷字符集,但是對PHP版本有要求;* mysql_escape_string不考慮連接的當(dāng)前字符集。
轉(zhuǎn)載請注明來源:php防止sql注入的函數(shù)介紹哈爾濱品用軟件有限公司致力于為哈爾濱的中小企業(yè)制作大氣、美觀的優(yōu)秀網(wǎng)站,并且能夠搭建符合百度排名規(guī)范的網(wǎng)站基底,使您的網(wǎng)站無需額外費(fèi)用,即可穩(wěn)步提升排名至首頁。歡迎體驗(yàn)最佳的哈爾濱網(wǎng)站建設(shè)。
