2015-11-14 100 views
-1

我讓PHP網站 使用用戶/密碼登錄 但用戶可以跳過這個頁面使用
「=」「或」
'或1 = 1 喜歡這個 下面的代碼文件Login_Check.php如何避免「=」「或」

`

include("includeszzz/host_conf.php"); 
include("includeszzz/mysql.lib.php"); 
$obj=new connect; 
$obj1=new connect; 
$username=$_GET["username"]; 
$password=$_GET["password"]; 
//echo $username; 
//echo $password; 
$sql="select username from admin where username='$username'"; 
$obj->query($sql); 
$U_num=$obj->num_rows(); 
//echo $U_num; 
if($U_num!=0) { 
$sql1="select password from admin where username='$username' and password='$password'"; 
$obj1->query($sql1); 
$P_num=$obj1->num_rows(); 
    if($P_num!=0) { 
     session_start(); 
     $_SESSION["zizo"]="$username"; 
    //header("location: welcome.php"); 
    echo "1"; 
} else { 
    echo "Invalid Password Please Try Again"; 
} 
} else { 
echo "Invalid Username Please Try Again"; 
} 

`

+1

你所經歷稱爲[** SQL注入**](https://www.owasp.org/index .PHP/SQL_Injection)。閱讀我剛纔發佈的鏈接和一個在@ NathanTuggy的評論。 –

回答

2

你要避免使用querie用戶數據•不用任何類型的衛生設施。 http://php.net/manual/en/security.database.sql-injection.php

「實施例#5所述的組成的查詢更安全的方式......」

<?php 

settype($offset, 'integer'); 
$query = "SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET $offset;"; 

// please note %d in the format string, using %s would be meaningless 
$query = sprintf("SELECT id, name FROM products ORDER BY name LIMIT 20 OFFSET %d;", 
       $offset); 

?> 
  • 如果數據庫層不支持綁定變量然後引用 的非數字用戶提供的值傳遞給數據庫 與特定數據庫字符串逃逸的功能(例如 mysql_escape_string()和sqlite_escape_string(),等等)。如addslashes()通用 功能只有在非常特殊的 環境(例如MySQL的一個單字節字符與殘疾人 NO_BACKSLASH_ESCAPES設置)是有用的,最好是避開他們。
  • 不要打印出任何數據庫的具體信息,特別是有關 架構,通過不擇手段。另請參見錯誤報告和錯誤處理 和日誌功能。
  • 您可以使用存儲過程和先前定義的光標 抽象數據訪問,以便用戶不直接訪問表或 視圖,但此解決方案具有另一個影響。

此外,您可以綁定參數的使用: http://php.net/manual/en/pdo.prepared-statements.php

<?php 
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)"); 
$stmt->bindParam(':name', $name); 
$stmt->bindParam(':value', $value); 

// insert one row 
$name = 'one'; 
$value = 1; 
$stmt->execute(); 

// insert another row with different values 
$name = 'two'; 
$value = 2; 
$stmt->execute(); 
?>