mysql防止SQL 注入

2021-07-09 02:32:46 字數 1153 閱讀 9031

(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; 

}

mysql中防止sql注入

什麼是sql注入 python 操作mysql產生sql注入問題 不用orm框架,框架中已經整合了防範sql注入的功能,使用pymysql實踐一下 匯入pymysql模組 import pymysql 連線database conn pymysql.connect host 127.0.0.1 us...

php 防止mysql注入 php防止sql注入

三個函式 addslashes string 用反斜線引用字串中的特殊字元 username addslashes username mysql escape string string 用反斜槓轉義字串中的特殊字元,用於mysql query 查詢。username mysql escape st...

防止SQL注入

1.什麼是sql注入 所謂sql注入,就是通過把sql命令插入到web表單遞交或輸入網域名稱或頁面請求的查詢字串,最終達到欺騙伺服器執行惡意的sql命令。通過遞交引數構造巧妙的sql語句,從而成功獲取想要的資料。2.sql注入的種類 從具體而言,sql注入可分為五大類,分別是 數字型注入 字元型注入...