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. I have tried everything I can think of, and I couldn't find anything on 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. 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. 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");