[英]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.