简体   繁体   English

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

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

I'm trying to use PHP to direct to a different form depending on the username & password combo entered: ssmith goes to main_menu.php & tsmith goes to menu.php. 我正在尝试使用PHP根据输入的用户名和密码组合将其定向到其他形式:ssmith转到main_menu.php,而tsmith转到menu.php。 I have tried everything I can think of, and I couldn't find anything on Google. 我已经尝试了所有我能想到的,但在Google上却找不到任何东西。 Does anyone know how to do this? 有谁知道如何做到这一点? When I click the 'login' button, no matter whether I put ssmith or tsmith, it always goes to main_menu.php. 当我单击“登录”按钮时,无论我放置ssmith还是tsmith,它总是转到main_menu.php。 Here's my current code: 这是我当前的代码:

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

}


}

}

?> 

first check the U letter in username. 首先检查用户名中的U字母。 is it the same name you used in the database. 它与您在数据库中使用的名称相同吗? otherwise use following code 否则使用以下代码

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

Maybe some regex would be helpful here with some security in mind. 考虑到一些安全性,也许一些正则表达式会有所帮助。

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.

相关问题 我创建了一个html提交按钮,并与php代码连接,但是,选择之后,我该如何返回我的html页面? - I have created one html submit button and connects with php code but, after selecting that how can i go back my html page? 我如何将值从两个html页面传递到php? - How can i pass values from two html pages to php? 如何将表单提交到两个不同的页面(不是同时)? - How can I submit a form to two different pages (not at same time)? 我的HTML表单将其操作链接到php代码。 但是,当单击“提交”按钮时,表单将重定向到PHP文件。 我该如何预防? - My HTML form has its action linked to php code. When the submit button is clicked though, the form redirects to the PHP file. How can I prevent this? 当我提交 HTML 表单时,我的 PHP 代码如何运行 - How can my PHP code run when i submit a HTML form 输入提交按钮后,如何更改从文本框中获取价值的HTML代码? - How can I change this html code that take value from text-box after enter submit button? 如何使我的ajax / javascript / php页面可抓取,可共享且可直接导航? - How can I make my ajax/javascript/php pages crawlable, shareable, and direct-to-navigateable? 我怎么能通过PHP从html代码获得第一个img - how can i get first img from html code by php 登录后如何将用户引导到不同的页面? - How can I direct users to different pages once they've logged in? 您如何从两个不同的php页面获取变量以在第三个php页面一起显示? - How can you get variables taken from two different php pages to show up together at at a third php page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM