亚洲韩日午夜视频,欧美日韩在线精品一区二区三区,韩国超清无码一区二区三区,亚洲国产成人影院播放,久草新在线,在线看片AV色

您好,歡迎來到思海網(wǎng)絡(luò),我們將竭誠為您提供優(yōu)質(zhì)的服務(wù)! 誠征網(wǎng)絡(luò)推廣 | 網(wǎng)站備案 | 幫助中心 | 軟件下載 | 購買流程 | 付款方式 | 聯(lián)系我們 [ 會員登錄/注冊 ]
促銷推廣
客服中心
業(yè)務(wù)咨詢
有事點擊這里…  531199185
有事點擊這里…  61352289
點擊這里給我發(fā)消息  81721488
有事點擊這里…  376585780
有事點擊這里…  872642803
有事點擊這里…  459248018
有事點擊這里…  61352288
有事點擊這里…  380791050
技術(shù)支持
有事點擊這里…  714236853
有事點擊這里…  719304487
有事點擊這里…  1208894568
有事點擊這里…  61352289
在線客服
有事點擊這里…  531199185
有事點擊這里…  61352288
有事點擊這里…  983054746
有事點擊這里…  893984210
當(dāng)前位置:首頁 >> 技術(shù)文章 >> 文章瀏覽
技術(shù)文章

MySQL和PHP中的SQL注入式漏洞解決方法

添加時間:2014-5-31 17:25:40  添加: 思海網(wǎng)絡(luò) 
SQL注入式漏洞是許多PHP程序的主要安全危害,產(chǎn)生的原因是在向數(shù)據(jù)庫執(zhí)行插入等語句時,web開發(fā)者允許最終用戶操作變量(例如根據(jù)表單提交內(nèi)容顯示相應(yīng)信息),通常是_GET、_POST或_SESSION等全局變量。


讓我們看以下的代碼:

以下為引用的內(nèi)容: <?PHP
query = "Select news_title, news_text ";
query .= "FROM news";
query .= "Where news_id=". _GET['id'];

mysql_query(query);
?>

如果認(rèn)為其中的_GET[‘id’]會永遠(yuǎn)是個數(shù)值型的值那將是很嚴(yán)重的錯誤。最終用戶可以改變這個變量的值,例如"0; Delete FROM news;",那么query語句就會變成下面的值:

Select news_title, news_text FROM news Where news_id=0; Delete FROM news;

這將產(chǎn)生很嚴(yán)重的后果。

驗證數(shù)值型數(shù)據(jù)

數(shù)值型數(shù)據(jù)是最容易驗證的,PHP有一個自帶的函數(shù)叫 is_numeric()可以返回ture值來判斷是否是數(shù)值型,這個函數(shù)并不是MySQL自帶的,因此可在任何數(shù)據(jù)庫平臺的php程序中用于驗證數(shù)字。

下面是修改后的代碼:

以下為引用的內(nèi)容: <?PHP
if (!is_numeric(_GET['id']))
{
// id's not numeric?
// kill the before the query can run
die("The id must be numeric!");
}

query = "Select news_title, news_text ";
query .= "FROM news";
query .= "Where news_id=". _GET['id'];

mysql_query(query);
?>

驗證非數(shù)值型數(shù)據(jù)

非 數(shù)值型數(shù)據(jù)的驗證稍有點麻煩。PHP有個叫Magic Quotes的特殊功能。當(dāng)它激活時,PHP會自動過濾掉_GET和_POST全局變量中的反斜線符號(\),雙引號(”),單引號(’)和空白字符。問 題是并不是所有的服務(wù)器都能打開了這個功能,所以必須檢測服務(wù)器是否開通了這個功能。可以使用get_magic_quotes_gpc()函數(shù)來判定 maigc quotes功能是否打開。
在MySQL查詢語句可以使用mysql_real_escape_string()函數(shù)來增強(qiáng)安全性,代碼如下:

以下為引用的內(nèi)容: <?PHP
// Fix a _POST variable called firstName for MySQL
firstName = _POST['firstName'];
if (get_magic_quotes_gpc())
{
// If magic quotes is enabled - turn the string back into an unsafe string
firstName = stripslashes(firstName);
}

// Now convert the unsafe string into a MySQL safe string
firstName= mysql_real_escape_string(firstName);

// firstName should now be safe to insert into a query
?>

輸出到頁面

為正確顯示字符中的引號和反斜線,應(yīng)使用stripslashes()函數(shù)

以下為引用的內(nèi)容: <?PHP
firstName = _POST['firstName'];
if (get_magic_quotes_gpc())
{
// If magic quotes is enabled - turn the string back into an unsafe string
firstName = stripslashes(firstName);
}

// Now convert the unsafe string into a MySQL safe string
firstName = mysql_real_escape_string(firstName);

// Safe query
mysql_query("Insert INTO Names VALUES('". firstName ."')");

// Page output should look proper
echo "Hello ". htmlentities(stripslashes(firstName));
?>

最終整合

最后可以建立一個簡單的函數(shù)來解決在PHP中如果安全的進(jìn)行MySQL查詢字符。值得注意的是,如果要輸出到WEB頁面上還需要使用stripslashes。

以下為引用的內(nèi)容: <?PHP
function VerifyInput(input, forceInt = false)
{
if (is_numeric(input))
{
return input;
}
elseif (!forceInt)
{
if (get_magic_quotes_gpc())
{
// if magic quotes is enabled, get rid of those
// pesky slashes
input = stripslashes(input);
}

// convert the input variable into a MySQL safe string.
input = mysql_real_escape_string(input);

return input;
}
else
{
// if input not an integer and forceInt = true,
// kill
die("Invalid Input");
}
}

// _POST['name'] should be a string
// _POST['id'] should be an integer, if not the dies
id = _POST['id'];
name = _POST['name'];

query = "Update users SET name=". VerifyInput(name) ." ";
query .= "Where id=". VerifyInput(id, true);

// query should be safe to run
mysql_query(query);

?>

關(guān)鍵字:MySQL、PHP、數(shù)據(jù)庫

分享到:

頂部 】 【 關(guān)閉
版權(quán)所有:佛山思海電腦網(wǎng)絡(luò)有限公司 ©1998-2024 All Rights Reserved.
聯(lián)系電話:(0757)22630313、22633833
中華人民共和國增值電信業(yè)務(wù)經(jīng)營許可證: 粵B1.B2-20030321 備案號:粵B2-20030321-1
網(wǎng)站公安備案編號:44060602000007 交互式欄目專項備案編號:200303DD003  
察察 工商 網(wǎng)安 舉報有獎  警警  手機(jī)打開網(wǎng)站