Redirect users to different pages based on role php

0 votes

Depending on the responsibilities that have been assigned to them in the database, I need to reroute users to various sites. On the login page, only the username and password are entered. I must retrieve the role from the database, which appears as follows:

username  |  password  |  role
xxxxxx       xxxxxx       admin
xxxxxx       xxxxxx       trainer
xxxxxx       xxxxxx       trainer
xxxxxx       xxxxxx       Line Manager

Here is my code:

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 


$sql="SELECT * FROM login WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

     // Register $myusername, $mypassword and redirect to file "login_success.php"
     $role = mysqli_fetch_array($result);


      if($role == "Admin"){
        header("location:index.php");
            exit();
      }
      elseif($role['role'] == "Trainer"){
  header("location:index1.php");
        exit();
     }
      elseif($role['role'] == "Line Manager"){
  header("location:index2.php");
        exit();
     }
      elseif($role['role'] == "Client"){
  header("location:client.php");
        exit();
     }


  }
     else {
        echo "Wrong Username or Password"; 
     }
?>

Can someone help me with this?

Jul 21, 2022 in PHP by narikkadan
• 63,600 points
4,373 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In PHP

0 votes
1 answer

How to resolve the problem of losing a session after a redirect in PHP?

Hello @kartik, Carry out these usual checks: Make sure session_start(); is ...READ MORE

answered Aug 24, 2020 in PHP by Niroj
• 82,840 points
35,150 views
0 votes
1 answer

How can I use Sockets.io on the client side and communicate with a PHP based application on the server?

Hello @kartik, For 'long-lived connection' , you can ...READ MORE

answered Aug 24, 2020 in PHP by Niroj
• 82,840 points
3,064 views
–1 vote
1 answer
0 votes
1 answer

How to generate file for download then redirect using php?

Hii, I don't think this can be done ...READ MORE

answered Oct 30, 2020 in PHP by Niroj
• 82,840 points
6,348 views
0 votes
2 answers

Define a SQL query? What is the difference between SELECT and UPDATE Query? How do you use SQL in SAS?

HI.. SQL is Structured Query Language, which is ...READ MORE

answered Aug 8, 2020 in PHP by anonymous
10,594 views
0 votes
1 answer

Why is not preferable to use mysql_* functions in PHP?

The reasons are as follows: The MySQL extension: Does ...READ MORE

answered Sep 7, 2018 in Database by DataKing99
• 8,250 points
1,284 views
0 votes
2 answers
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP