根据输入的用户名和密码组合,我尝试使用PHP指向不同的表单: ssmith转到main_menu.php,tsmith转到menu.php。我试过所有我能想到的东西,但我在谷歌上找不到任何东西。有人知道怎么做吗?当我点击“登录”按钮时,不管我放的是ssmith还是tsmith,它总是转到main_menu.php。下面是我当前的代码:
<?php
//Connect to database
include "db_connect.php";
//Get variable from login form
$username=$_POST['username'];
$password=$_POST['password'];
//Check if any text has been entered in username and password
if ((empty($username)) || empty($password)){
echo "Please enter a username or password. Go back to the <a
href='Index.php'>login page</a>";
}
else {
//Check to see if username and password is found in table
$sql="SELECT * FROM accounts WHERE Username='ssmith'";
//Place the result of the sql query into the variable $result
$result=$mysqli->query($sql);
if($result=ssmith){
//Display main menu page
header("location:main_menu.php");
}
else {
//Check to see if username and password is found in table
$sql="SELECT * FROM accounts WHERE Username='tsmith'";
//Place the result of the sql query into the variable $result
$result=$mysqli->query($sql);
if($result=tsmith){
//Display menu page
header("location:menu.php");
}
else {
//Display error message
echo "Please username and password could not be found. Go back to the <a
href='Index.php'>login page</a>";
}
}
}
?>
发布于 2018-10-20 11:34:03
首先检查用户名中的U字母。是不是和你在数据库里用的名字一样。否则,请使用以下代码
//first search anything from accounts
$sql="SELECT * FROM accounts";
//then navigate header according to the result
$result_query=$mysqli->query($sql);
if (mysqli_num_rows($result_query) > 0){
while($result = mysqli_fetch_assoc($result_query)){
if($result['username'] == 'ssmith'){
header("location:main_menu.php");
}elseif($result['username'] == 'tsmith'){
header("location:menu.php");
}
}
}
发布于 2018-10-20 12:18:57
也许在考虑到安全性的情况下,一些正则表达式可能会有所帮助。
if(preg_match("/ssmith/", $result)){
$url = "main_menu.php";
}
else if(preg_match("/tsmith/", $result)){
$url = "menu.php";
}
else{
echo "Please username and password could not be found. Go back to the <a
href='Index.php'>login page</a>";
}
header("location: $url");
https://stackoverflow.com/questions/52902089
复制相似问题