2017-11-25 79 views
-1

我試圖使用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和密碼存在於數據庫中,但它說登錄失敗。所以請幫我解決這個問題....

+0

使用'$ _GET'而不是'$ _POST' ..我強烈建議您使用準備好的語句作爲你在你的查詢中有一個'WHERE'子句,請不要用明文存儲密碼..因此請不要使用這段代碼。你要讓用戶處於危險的境地 – Akintunde007

+0

否否否.... BIG NO,此代碼不能獲得任何LESS安全,您的SQL不應包含用戶輸入,您不會使用'$ _GET'傳遞敏感數據! – Samuel

+0

也打開SQLi - [我怎樣才能防止在SQL注入PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1) –

回答

-1

您的鏈接代碼是$ _GET不是$ _POST。我會建議改變這個帳戶來代替,但爲了學習,你可以簡單地將所有$_POST改爲$_GET

+0

如何在鏈接中使用$ _POST方法 –

+0

您不能在鏈接中使用$ _POST方法,並且在進一步深入研究之前,我會建議您進一步研究安全方法。有幾個安全問題。有關$ _POST的更多信息,您可以訪問http://php.net/manual/en/reserved.variables.post.php –