防止SQL注入的方法

2022-05-22 11:12:08 字數 922 閱讀 9421

方法1⃣️addslashes();

$username=addslashes($_post['username']);

方法2⃣️mysql_escape_string();

方法3⃣️開啟魔術引號

方法4⃣️控制使用者輸入的資料,如:

使用intval防止sql注入,intval()可以把變數轉成整數型別,適用於接收純整數資料

$id=intval($_get['id']);

方法5⃣️用pdo物件的quote()方法控制使用者輸入的資料:

會返回帶引號的字串,並通過反斜線轉義來過濾字串中的特殊字元

用法:  $username=$_post(username);

$username=$pdo->quote($username);

方法6⃣️用pdo物件的prepare()、excute()方法:

用法:命名佔位符形式1⃣️:

$username=$_post(username);

$passwd=$_post(passwd);

$sql="select * from users where username=:a and passwd=:b ;"

;$stmt=$pdo->prepare($sql);

$stmt->execute(array(:a=>$username,:b=>$passwd));

問號佔位符形式2⃣️:

$username=$_post(username);

$passwd=$_post(passwd);

$sql="select * from users where username=? and passwd=? ;";

$stmt=$pdo->prepare($sql);

$stmt->execute(array($username,$passwd));

防止sql注入方法

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

防止SQL注入的方法

防止sql注入的3個方法 方法一 引數化查詢。缺點 增大資料庫的壓力 string connectionstring 資料庫連線字串 string customerid string empty string companyname string empty string address strin...

防止SQL注入的方法

防止sql注入的3個方法 方法一 引數化查詢。缺點 增大資料庫的壓力 string connectionstring 資料庫連線字串 string customerid string empty string companyname string empty string address strin...