简体   繁体   中英

I have two types of users - User and Admin. I would like to include a session check, to check if the person is logging on as a user or as an admin.

This is the code I've included :

<?php
session_start();
if(!isset($_SESSION['userid']) OR $_SESSION['userid']==''){

   header('Location: Loginhome.php');

}
elseif(isset($_SESSION['user_type']) && $_SESSION['user_type'] =='Admin'){
     header('Location: adminhome.php');

}
else{
     header('Location: userhome.php');
   exit;
}

?>

And this code says that the page isn't redirecting properly after logging in.

 <?php
 session_start();
 if(!isset($_SESSION['user_type']=='1']) OR $_SESSION['userid']==''){

    header('Location: Loginhome.php');

  }
 else
if(isset($_SESSION['user_type']=='2']) && $_SESSION['user_type'] =='Admin'){
     header('Location: adminhome.php');

 }
 else{
   header('Location: userhome.php');
   exit;
   }

 ?>

you need to do like this things

you need to helpful this one

declare a role

//for admin

if ($_SESSION['user']['role'] == 1) {

//your code here

}

//for user all

if ($_SESSION['user']['role'] == 2 && $_SESSION['user']['name'] == $username) {

//your code here

}

good luck guys

<?php

session_start();

if ($_SESSION['role'] !== 'admin') {
  header('Location: ../index');

<?php
require_once('config.php');
require_once('classes/admin.class.php');
require_once('classes/log.class.php');
$OBJ_LOGIN = new ADMIN();
$OBJ_LOG = new LOG();

//print_r($_SESSION);
if($OBJ_LOGIN->LoggedUser())
{
redirect('dashboard.php');
}


//$res_login = array();
$flg=0;

$action = $_GET['a'];
if($action == 'logout')
{
// $sql1 = "INSERT INTO log VALUES(NULL,'".$_SESSION['userempname']."','Log out of the System',NOW())";

$OBJ_LOG->userName = $_SESSION['userfullname'];
$OBJ_LOG->logText = 'Log out of the System';
$res_log = $OBJ_LOG->Addlog();

// mysql_query($sql1);

if(session_destroy())
{
redirect('index.php');
}
}


if($_POST['logfrm'] == 'yes')
{
$OBJ_LOGIN->aUsername = $_POST['uname'];
$OBJ_LOGIN->aPassword = md5($_POST['upwd']);

$res_login = $OBJ_LOGIN->LoginUser();
//print_r($res_login);

if($_POST['uname'] == $res_login[0]['aUsername'] && md5($_POST['upwd']) == $res_login[0]['aPassword'])
{
$_SESSION['adminid'] = $res_login[0]['adminID'];
$_SESSION['userrole'] = $res_login[0]['aType'];
$_SESSION['useremail'] = $res_login[0]['aEmail'];
$_SESSION['login_user'] = $res_login[0]['aUsername'];
$_SESSION['userpass'] = $res_login[0]['aPassword'];
$_SESSION['userfullname'] = $res_login[0]['aFullname'];
$_SESSION['userimg'] = $res_login[0]['aImage'];

$OBJ_LOG->userName = $_SESSION['userfullname'];
$OBJ_LOG->logText = 'Login into the System';
$log = $OBJ_LOG->Addlog();
redirect('dashboard.php');
}
else
{
$flg = 1;
}
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM