简体   繁体   English

我有两种类型的用户 - 用户和管理员。 我想包括一个会话检查,以检查该人是以用户身份登录还是以管理员身份登录。

[英]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) {如果($_SESSION['user']['role'] == 1){

//your code here //你的代码在这里

} }

//for user all //对于所有用户

if ($_SESSION['user']['role'] == 2 && $_SESSION['user']['name'] == $username) { 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 <?php
require_once('config.php'); require_once('config.php');
require_once('classes/admin.class.php'); require_once('classes/admin.class.php');
require_once('classes/log.class.php'); require_once('classes/log.class.php');
$OBJ_LOGIN = new ADMIN(); $OBJ_LOGIN = 新管理员();
$OBJ_LOG = new LOG(); $OBJ_LOG = 新日志();

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


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

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

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

// mysql_query($sql1); // mysql_query($sql1);

if(session_destroy())如果(会话销毁())
{ {
redirect('index.php');重定向('index.php');
} }
} }


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

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

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

$OBJ_LOG->userName = $_SESSION['userfullname']; $OBJ_LOG->userName = $_SESSION['userfullname'];
$OBJ_LOG->logText = 'Login into the System'; $OBJ_LOG->logText = '登录系统';
$log = $OBJ_LOG->Addlog(); $log = $OBJ_LOG->Addlog();
redirect('dashboard.php');重定向('dashboard.php');
} }
else别的
{ {
$flg = 1; $flg = 1;
} }
} }
?> ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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