簡體   English   中英

登錄頁面不會重定向到主頁

[英]Login page doesn't redirect to homepage

我有以下腳本:

//Possible solution for logging a user in below. 
if($result = mysqli_query($db,"SELECT password FROM USERS WHERE password = '". $password ."' AND username = '". $username ."'")){
    $count = $result->num_rows;
    $accounttype = mysqli_query($db, "SELECT AccountType FROM USERS WHERE username ='". $username."'");
    if($count>=1){
        if($accounttype == "tradesmen"){
            include('../html/WelcomeCustomer.html');
        }
        else if($accounttype == "customer"){
            include('../html/WelcomeTrade.html');
        }
    }
    else{
        echo "The login credentials were incorrect";
    }
}

我知道這個實現在安全性方面有很多問題。 我計划在登錄后修復此問題。

現在我只想讓用戶登錄。當我運行它時,我得到一個空白頁面,使用以下帳戶:

username, password, AccountType: test, test, tradesmen. 
username, password, AccountType: work, work, customer.

我打開了錯誤報告,但什么也沒得到。 腳本不重定向用戶的原因可能是什么?

好吧,您沒有redirecting控件。 該聲明include('../html/WelcomeCustomer.html'); 將只include HTML文件,而不是redirecting到它。

所以你需要做以下。 你也不需要雙重查詢數據庫

$mysqli = new mysqli("localhost", "my_user", "my_password", "db_name");
$result = $mysqli->query($db,"SELECT * FROM USERS WHERE password = '". $password ."' AND username = '". $username ."'");
$count = $result->num_rows;
if($count){
    $row = $result->fetch_assoc();
    if($row['AccountType'] == "tradesmen"){
        header('Location: ../html/WelcomeCustomer.html');
    }
    else if($row['AccountType'] == "customer"){
        header('Location: ../html/WelcomeTrade.html');
    }
}
else{
    echo "The login credentials were incorrect";
}

}

只需更改您的include語句,我發現您直接匹配if條件中的object ,您需要先fetch數據,然后再進行比較。

重要的

在某些地方,您使用的是面向對象的樣式,例如$result->num_rows; 在某些地方,您使用程序風格,mysqli_query 所以我建議你使用其中之一,我已經用面向對象的風格更新了我的答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM