Home > Blockchain >  How to redirect 2 users from different homepage using php
How to redirect 2 users from different homepage using php

Time:10-03

I want to redirect users where if users role is trainees they will go to = trainees/index.php if admin then admin/index.php

login.php code

<?php
include("php/dbconnect.php");

$error = '';
if(isset($_POST['login']))
{

$username =  mysqli_real_escape_string($conn,trim($_POST['username']));
$password =  mysqli_real_escape_string($conn,$_POST['password']);

if($username=='' || $password=='')
{
$error='All fields are required';
}

$sql = "select * from user where username='".$username."' and password = '".md5($password)."'";

$q = $conn->query($sql);
if($q->num_rows==1)
{
$res = $q->fetch_assoc();
$_SESSION['rainbow_username']=$res['username'];
$_SESSION['rainbow_uid']=$res['id'];
$_SESSION['rainbow_name']=$res['name'];
$_SESSION['rainbow_role']=$res['role'];
echo '<script type="text/javascript">window.location="index.php"; </script>';

}else
{
$error = 'Invalid Username or Password';
}

}

?>

CodePudding user response:

javascript is not really needed here, you can use header("Location: .."); to redirect the page

if ($q->num_rows == 1)
{
    $res = $q->fetch_assoc();
    $_SESSION['rainbow_username'] = $res['username'];
    $_SESSION['rainbow_uid'] = $res['id'];
    $_SESSION['rainbow_name'] = $res['name'];
    $_SESSION['rainbow_role'] = $res['role'];
    if ($res['role'] == 'trainees')
    {
        header("Location: trainees/index.php");
    }
    elseif ($res['role'] == 'admin')
    {
        header("Location: admin/index.php");
    }
    else
    {
        header("Location: member.php");
    }
    // stop php execution
    exit();
}
else
{
    $error = 'Invalid Username or Password';
}

CodePudding user response:

Adding a user level field to your user TABLE. Maybe:

guests are level 0 Members are 101 Admins are 102

After the login procedure, you can check the user level and redirected to different pages.

  •  Tags:  
  • php
  • Related