簡體   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