0

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

}


}

}

?> 
Dunk_K
  • 11
  • 2
  • Please check this link. https://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php. – Lead Developer Oct 20 '18 at 03:15
  • 1
    `if($result=ssmith){` this is wrong 3 ways $result is an object. comparison is `==` not `=` and `tmsith` should be quoted –  Oct 20 '18 at 03:19

2 Answers2

0

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");
        }
    }
}
Anuradha
  • 341
  • 1
  • 3
  • 7
  • comparison is `==` not `=` header location should have space after the colon and location has a capital L –  Oct 20 '18 at 03:48
  • I copy-pasted this code in (so there were no mistakes) to try it, but I'm just getting an error that says 'Cannot use object of type mysqli_result as array in line 21' - that's the if($result['username'] == ssmith'){ line. I'm not sure what to do about this? – Dunk_K Oct 20 '18 at 05:02
0

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");
norcal johnny
  • 2,050
  • 2
  • 13
  • 17