最全防止sql注入方法

2021-07-09 12:51:19 字數 1524 閱讀 2741

(1)mysql_real_escape_string -- 轉義 sql 語句中使用的字串中的特殊字元,並考慮到連線的當前字符集 

使用方法如下:

$sql 

="select count(*) as ctr from users where username

='".

mysql_real_escape_string

($username

)."' and

password='"

.mysql_real_escape_string

($pw

)."' limit 1"

;

使用mysql_real_escape_string()作為使用者輸入的包裝器,就可以避免使用者輸入中的任何惡意 sql 注入。

(2) 開啟magic_quotes_gpc來防止sql注入

php.ini中有乙個設定:magic_quotes_gpc = off

這個預設是關閉的,如果它開啟後將自動把使用者提交對sql的查詢進行轉換,

比如把 ' 轉為 \'等,對於防止sql注射有重大作用。

如果magic_quotes_gpc=off,則使用addslashes()函式

(3)自定義函式

function

inject_check

($sql_str

)function

verify_id

($id

=null

)elseif

(inject_check

($id

))elseif

(!is_numeric

($id

))$id

=intval

($id

);return

$id;

}function

str_check

($str

)$str

=str_replace

("_"

,"\_"

,$str

);$str

=str_replace

("%"

,"\%"

,$str

);return

$str;}

function

post_check

($post

)$post

=str_replace

("_"

,"\_"

,$post

);$post

=str_replace

("%"

,"\%"

,$post

);$post

=nl2br

($post

);$post

=htmlspecialchars

($post

);return

$post;}

最全防止sql注入方法

1 mysql real escape string 轉義 sql 語句中使用的字串中的特殊字元,並考慮到連線的當前字符集 使用方法如下 sql select count as ctr from users where username mysql real escape string userna...

最全防止sql注入方法

1 mysql real escape string 轉義 sql 語句中使用的字串中的特殊字元,並考慮到連線的當前字符集 使用方法如下 sql select count as ctr from users where username mysql real escape string userna...

最全防止sql注入方法

1 mysql real escape string 轉義 sql 語句中使用的字串中的特殊字元,並考慮到連線的當前字符集 使用方法如下 sql select count as ctr from users where username mysql real escape string userna...