Home > OS >  Creating Php pages for multiple user types
Creating Php pages for multiple user types

Time:12-11

I need to create a website where different user can login and for each type user, they will have same and also different buttons. I have a homepage(for a visitor of if a user has not logged in, an admin homepage for admin login with different features or buttons, a student homepage for student login with different buttons or features.

Login code

<?php
include('../database.php');
$email = '';
$password = '';
$login_type = '';

if (isset($_GET['type']) && isset($_POST['email']) && isset($_POST['password'])) {
    $email = $_POST['email'];
    $password = $_POST['password'];
    $login_type = $_GET['type'];
}

$query = "SELECT * FROM accounts WHERE email = ? AND type= ?;";

$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, "ss", $email, $login_type);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

$current_user = mysqli_fetch_assoc($result);

if (!password_verify($password, $current_user['password'])) {
    //Wrong credentials   
    header("Location: ../LoginPage.php?error=401&type=$login_type");
} else {
    //Login successful. Creates session of the user.
    session_start();
    $_SESSION['firstname'] = $current_user['firstname'];
    $_SESSION['lastname'] = $current_user['lastname'];
    $_SESSION['logintype'] = $current_user['type'];

    if ($current_user['type'] == 'admin') {
        header('Location: ../AdminHome.php?usertype="admin"');
    } else if ($current_user['type'] == 'student') {
        header('Location: ../StudentHome.php?usertype="student"');
    } else {
        die('404: File not Found!');
    }
}

homepage for visitor is just plain html code

homepage for admin login

<?php
include('database.php');
$usertype= "";

    session_start();
if ($_SESSION['firstname'] && $_SESSION['lastname']) {
  $firstname = $_SESSION['firstname'];
  $lastname = $_SESSION['lastname'];
  
} else {
  header('Location: LibraryHome.php?login=false');
}
?>

This is also the same for the student homepage

now my problem is they have same buttons like

home button browse button login button

and they have the different buttons on their top nav

                         visitorbuttons   visitorbuttons

homebutton

browse button

login button

                      adminbuttons   adminbuttons

homebutton

browse button

login button

                      studentbuttons  studentbuttons                  

homebutton

browse button

login button

I want to create the pages in a way that i will not need to create many pages whenever i click buttons for different users type

for example, if i login as admin and clicked browse button, i will see its own top nav, but if i login as student , i will see a different top nav buttons, and for this to do, i need to create a browse page for each of different user type.this is also the case for login and home button.

Is there any way to do this optimally without creating too many pages?

CodePudding user response:

One way to do this would be to use the $_SESSION['logintype'] to define what content to show by having buttons wrapped in conditions like:

<?php
    if ($_SESSION['logintype'] === 'admin') {
        echo "Element that can only be seen by admins";
    }
?>

You then only need one page but you can define different headers, navbars, buttons, anything you like depending on the user type.

  • Related