我試圖使用PHP和MySQL在XAMPP服務器進行登陸驗證,但我有驗證問題,即使輸入了無效值,它顯示正確的登錄驗證使用PHP和MySQL不工作
這裏是登錄。 PHP文件
<?php
$localhost = "127.0.0.1:3307";
$username = "root";
$password = "";
$dbname = "sample";
$conn = mysqli_connect($localhost, $username, $password, $dbname);
if(!$conn){
die("Connection failed :". mysqli_connect_error());
}
$dbselection = mysqli_select_db($conn, $dbname);
if(!$dbselection){
die("Database select failed :". mysqli_connect_error());
}
$user_id = "";
$pwd = "";
if(isset($_POST['user_id'])){
$user_id = $_POST['user_id'];
}
if(isset($_POST['pwd'])){
$pwd = $_POST['pwd'];
}
$query = "select username from student_info where username ='$user_id' and password ='$pwd'";
$result = mysqli_query($conn, $query);
if(mysqli_num_rows($result) > 0){
echo "success";
} else {
echo "failed";
}
mysqli_close($conn);
?>
這裏是如何我測試的文件
http://localhost:8080/sample/login.php?user_id=student1&pwd=admin
在上面的url user_id和密碼存在於數據庫中,但它說登錄失敗。所以請幫我解決這個問題....
使用'$ _GET'而不是'$ _POST' ..我強烈建議您使用準備好的語句作爲你在你的查詢中有一個'WHERE'子句,請不要用明文存儲密碼..因此請不要使用這段代碼。你要讓用戶處於危險的境地 – Akintunde007
否否否.... BIG NO,此代碼不能獲得任何LESS安全,您的SQL不應包含用戶輸入,您不會使用'$ _GET'傳遞敏感數據! – Samuel
也打開SQLi - [我怎樣才能防止在SQL注入PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1) –