-1

I need to do a multi userlogin, so the admin will be logged into admin.php and the clerk will be redirected into clerk.php.

But I am having a issue. I have coded it so far and it lets me redirect clerk to clerk.php. But my admin login just returns me back to the index.php. Github if you want a better idea. https://github.com/markRichie/MSS

This is my code below.

Functions.php:

   if(mysqli_num_rows($result) > 0)
    {
        while($row =mysqli_fetch_assoc($result))
        {
            if($row["Role"] == "admin")
        {
           $_SESSION['User'] = $row["username"];
           $_Session['Role'] = $row["Role"];
           header('Location: admin.php');
        }
        else
        {
            $_SESSION['User'] = $row["username"];
            $_Session['Role'] = $row["Role"];
            header('Location: clerk.php');
        }
    }

    }
    else {
        header('Location: index.php');
    }
}
?>

Admin.php:


<?php
session_start();
if(isset($_SESSION['Role']))
{
  if($_SESSION['Role'] != 'admin')
  {
    header('Location: clerk.php');
  }
}
else
{
  header('Location: index.php');
}

And my table:

CREATE TABLE `multilogin` (
  `ID` int(11) NOT NULL,
  `username` varchar(30) NOT NULL,
  `password` varchar(30) NOT NULL,
  `Role` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • This could mean that the query is returning 0 result, can you add your MySQL query? – Michael Tétreault Dec 16 '19 at 19:49
  • I don't see where the OP received an "Undefined variable" notice or any notice, so I am not sure why this question was closed as a duplicate. – Booboo Dec 16 '19 at 20:22
  • Yeah, no clue why they closed my thread. It had nothing to do with the similar thread... – Nathan De Lima Dec 16 '19 at 20:28
  • @NathanDeLima It has *everything* to do with it. Undefined, plain and simple. Whoever voted to reopen, should not have. If it gets reopened, I will flag for moderation, and/or post on meta. – Funk Forty Niner Dec 16 '19 at 20:56

1 Answers1

0

Not:

$_Session['Role'] = $row["Role"];

But rather:

$_SESSION['Role'] = $row["Role"];

You misspelled $_SESSION in two places in file Functions.php.

Code to handle multiple roles:

You need to code for each role a separate file such as admin.php, clerk,php, doctor.php, patient.php, etc. and then:

Functions.php:

if(mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    $_SESSION['User'] = $row['username'];
    $role = $row['role'];
    $_SESSION['Role'] = $role;
    // $role is 'admin' or 'clerk' or 'doctor' or 'patient' etc.
    header("Location: $role.php");
}
else {
    header('Location: index.php');
}
Booboo
  • 38,656
  • 3
  • 37
  • 60
  • Thank you so much bro. Appreciate it. I can't believe I missed that. Adding onto one more question. If I were to add more multi-users. E.g Doctors, patients and so forth. What would I have to change or where would I start? I assume in functions.php I need to add another IF? Thank you again. – Nathan De Lima Dec 16 '19 at 20:01
  • 1
    I've updated my answer on how I would probably handle multiple roles. – Booboo Dec 16 '19 at 20:12
  • 1
    Somebody downvoted me -- not sure why. Feel free to accept this as the valid answer. – Booboo Dec 16 '19 at 20:16
  • Thanls bro. I upvoted your stuff. Really appreciate it. – Nathan De Lima Dec 16 '19 at 20:28
  • Yes, but somebody offset your upvote with a downvote. See [How does accepting an answer work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). No pressure. – Booboo Dec 16 '19 at 20:31