PHP登錄環(huán)節(jié)防止sql注入的方法淺析
在防止sql注入這些細節(jié)出現(xiàn)問題的一般是那些大意的程序員或者是新手程序員,他們由于沒有對用戶提交過來的數(shù)據(jù)進行一些必要的過濾,從而導致了給大家測試的時候一下就攻破了你的數(shù)據(jù)庫,下面我們來簡單的介紹一個用戶登錄未進行安全配置可能出現(xiàn)的sql注入方法,下面一起來看看吧。
比如以下一段登錄的代碼:
if($l = @mysql_connect('localhost', 'root', '123')) or die('數(shù)據(jù)庫連接失敗');
mysql_select_db('test');
mysql_set_charset('utf8');
$sql = 'select * from test where username = "$username" and password = "$password"';
$res = mysql_query($sql);
if(mysql_num_rows($res)){
header('Location:./home.php');
}else{
die('輸入有誤');
}
注意上面的sql語句,存在很大的安全隱患,如果使用以下萬能密碼和萬能用戶名,那么可以輕松進入頁面:
$sql = 'select * from test where username = "***" and password = "***" or 1 = "1"';
很明顯,針對這條sql語句的萬能密碼是: ***" or 1 = "1
$sql = 'select * from test where username ="***" union select * from users/* and password = "***"';
正斜線* 表示后面的不執(zhí)行,mysql支持union聯(lián)合查詢,因此直接查詢出所有數(shù)據(jù); 所以針對這條sql語句的萬能用戶名是:***" union select * from users/*
但是,此注入只針對代碼中的sql語句,如果
$sql = "select * from test where username = $username and password = $password";
上面的注入至少已經不管用了,不過方法是一樣的;
在使用PDO之后,sql注入完全可以被避免,而且在這個快速開發(fā)的時代,框架橫行,已然不用過多考慮sql注入問題了。
下面整理了兩個防止sql注冊函數(shù)
/* 過濾所有GET過來變量 */
foreach ($_GET as $get_key=>$get_var)
{
if (is_numeric($get_var)) {
$get[strtolower($get_key)] = get_int($get_var);
} else {
$get[strtolower($get_key)] = get_str($get_var);
}
}
/* 過濾所有POST過來的變量 */
foreach ($_POST as $post_key=>$post_var)
{
if (is_numeric($post_var)) {
$post[strtolower($post_key)] = get_int($post_var);
} else {
$post[strtolower($post_key)] = get_str($post_var);
}
}
/* 過濾函數(shù) */
//整型過濾函數(shù)
function get_int($number)
{
return intval($number);
}
//字符串型過濾函數(shù)
function get_str($string)
{
if (!get_magic_quotes_gpc()) {
return addslashes($string);
}
return $string;
}
另外還有一些博客會這樣寫
<?php
function post_check($post)
{
if (!get_magic_quotes_gpc()) // 判斷magic_quotes_gpc是否為打開
{
$post = addslashes($post); // 進行magic_quotes_gpc沒有打開的情況對提交數(shù)據(jù)的過濾
}
$post = str_replace("_", "\_", $post); // 把 '_'過濾掉
$post = str_replace("%", "\%", $post); // 把' % '過濾掉
$post = nl2br($post); // 回車轉換
$post= htmlspecialchars($post); // html標記轉換
return $post;
}
?>
- php中防止SQL注入的最佳解決方法
- php防止SQL注入詳解及防范
- PHP中防止SQL注入攻擊和XSS攻擊的兩個簡單方法
- PHP簡單實現(xiàn)防止SQL注入的方法
- 淺析php過濾html字符串,防止SQL注入的方法
- php防止sql注入示例分析和幾種常見攻擊正則表達式
- 探討php中防止SQL注入最好的方法是什么
- PHP+mysql防止SQL注入的方法小結
- php+mysqli預處理技術實現(xiàn)添加、修改及刪除多條數(shù)據(jù)的方法
- php+mysqli使用預處理技術進行數(shù)據(jù)庫查詢的方法
- PHP mysqli擴展庫 預處理技術的使用分析
- PHP防止sql注入小技巧之sql預處理原理與實現(xiàn)方法分析
相關文章
php基于環(huán)形鏈表解決約瑟夫環(huán)問題示例
這篇文章主要介紹了php基于環(huán)形鏈表解決約瑟夫環(huán)問題,結合具體實例形式分析了php環(huán)形鏈表的定義及基于環(huán)形鏈表解決約瑟夫環(huán)的具體步驟與相關操作技巧,需要的朋友可以參考下2017-11-11
PHP實現(xiàn)將多個文件壓縮成zip格式并下載到本地的方法示例
這篇文章主要介紹了PHP實現(xiàn)將多個文件壓縮成zip格式并下載到本地的方法,涉及php針對文件與目錄的讀寫、判斷與zip壓縮相關操作技巧,需要的朋友可以參考下2018-05-05

