gpt4 book ai didi

php - 如何让我的 html/php 代码从 1 个提交按钮定向到两个不同的页面?

转载 作者:行者123 更新时间:2023-11-29 16:49:17 25 4
gpt4 key购买 nike

我正在尝试使用 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>";

}


}

}

?>

最佳答案

考虑到安全性,也许一些正则表达式会有所帮助。

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");

关于php - 如何让我的 html/php 代码从 1 个提交按钮定向到两个不同的页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52902089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com