繁体   English   中英

如何通过1个提交按钮将html / php代码定向到两个不同的页面?

[英]How can I get my html/php code to direct to two different pages from 1 submit button?

我正在尝试使用PHP根据输入的用户名和密码组合将其定向到其他形式:ssmith转到main_menu.php,而tsmith转到menu.php。 我已经尝试了所有我能想到的,但在Google上却找不到任何东西。 有谁知道如何做到这一点? 当我单击“登录”按钮时,无论我放置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>";

}


}

}

?> 

首先检查用户名中的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");
        }
    }
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM