-2

please I need help with this php multi-user redirect code. when you

log in is able to access the database but I doest redirect the user to its

page.

<?php 
 require 'database-config.php';
 session_start();
 $username = "";
 $password = "";
 if(isset($_POST['username'])){
  $username = $_POST['username'];
 }
 if (isset($_POST['password'])) {
  $password = $_POST['password'];
 }
 $q = 'SELECT * FROM user_mgnt WHERE username=:username AND password=:password';
 $query = $dbh->prepare($q);
 $query->execute(array(':username' => $username, ':password' => $password));
 if($query->rowCount() == 0){
  header('Location: index.php?err=1');
 }else{
  $row = $query->fetch(PDO::FETCH_ASSOC);
  session_regenerate_id();
  $_SESSION['sess_user_id'] = $row['id'];
  $_SESSION['sess_username'] = $row['username'];
  $_SESSION['sess_userrole'] = $row['role'];
         echo $_SESSION['sess_username'];
         echo $_SESSION['sess_userrole'];
         session_write_close();
  if( $_SESSION['sess_userrole'] == "Admin"){
   header('Location: Dashboard.php');
  }
  elseif ( $_SESSION['sess_userrole'] == "Employee"){
   header('Location: emp_dashboard.php');
  }
  elseif ( $_SESSION['sess_userrole'] == "Registrar"){
   header('Location: Registration_Dashboard.php');
  }
  elseif ( $_SESSION['sess_userrole'] == "Accountant"){
   header('Location: Account_Dashboard.php');
  }
   else{
   header('Location: emp_dashboard.php');
  }
 }
?>
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 2
    What does it do? Instead of `header`s do `echo`s so you see what it executes. You also should be hashing your passwords. – chris85 Apr 24 '18 at 04:47
  • Then what happens if it doesn't? Tell us more – NoOorZ24 Apr 24 '18 at 04:55
  • it only displays the role and username but it doesn't show the admin or user page – Dennis jubin Apr 24 '18 at 04:56
  • But if you can help me with a different code i will be grateful – Dennis jubin Apr 24 '18 at 04:57
  • So you get to the `echo $_SESSION['sess_username'];`? If so you can't output before a `header`. Enable error reporting and this thread will become relevant, https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php. – chris85 Apr 24 '18 at 05:10

1 Answers1

0

try with this

<?php 
session_start();
require 'database-config.php';

$username = "";
$password = "";
if(isset($_POST['username'])){
    $username = $_POST['username'];
}
if (isset($_POST['password'])) {
    $password = $_POST['password'];
}

$q = 'SELECT * FROM user_mgnt WHERE username=:username AND password=:password';
$query = $dbh->prepare($q);
$query->execute(array(':username' => $username, ':password' => $password));

if($query->rowCount() == 0){
    header('Location: index.php?err=1');
}else{
    $row = $query->fetch(PDO::FETCH_ASSOC);

    $_SESSION['sess_user_id'] = $row['id'];
    $_SESSION['sess_username'] = $row['username'];
    $_SESSION['sess_userrole'] = $row['role'];

    switch($row['role']){
        case 'Admin' : $redirectUrl = 'Dashboard.php'; break;       
        case 'Registrar' : $redirectUrl = 'Registration_Dashboard.php'; break;      
        case 'Accountant' : $redirectUrl = 'Account_Dashboard.php'; break;      
        case 'Employee' : default : $redirectUrl = 'emp_dashboard.php'; break;
    }
    header('Location: ' . $redirectUrl);
    exit();
}
?>
Anfath Hifans
  • 1,588
  • 1
  • 11
  • 20
  • thank you ... but it still didn't work ... it only displays this [ Admin] but not the admin page [Dashboard.php] – Dennis jubin Apr 24 '18 at 05:43
  • @Dennisjubin, i think in your `user_mgnt` there is no `role` column... that's php script not checking the condition.. could you please tell me the your `user_mgnt` table structure not a values. only structure or check yourself – Anfath Hifans Apr 24 '18 at 06:11
  • CREATE TABLE IF NOT EXISTS `user_mgnt` ( `id` int(11) NOT NULL, `username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `role` varchar(255) NOT NULL, `name` varchar(255) NOT NULL, `Email` varchar(255) NOT NULL, `Status` varchar(255) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=40013 DEFAULT CHARSET=latin1; – Dennis jubin Apr 24 '18 at 07:21
  • Hifan is now working on my localhost but not working in my domain – Dennis jubin Apr 24 '18 at 07:24
  • then, check database connection such as username and password database in your domain – Anfath Hifans Apr 24 '18 at 07:25
  • I have done that .. but can you get me a new code .. I will be grateful – Dennis jubin Apr 24 '18 at 08:56