Jump to content

If user not admin redirect


slj90

Recommended Posts

I currently have a code to see if the user is logged in,

 

<?
// Check if we have an authenticated user
if (!isset($_SESSION["authenticatedUser"]))
//if not re-direct to login page
{
$_SESSION["message"] = "Please Login";
header("Location: loginpage.php");
}
else;
{
//If authenticated then display page conetents
?>

 

How can I change it for my admin page, so if the users username (authenticatedUser) isn't 'Admin' it tkaes them back to the login?

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/188868-if-user-not-admin-redirect/
Share on other sites

<?php
session_start();
// Check if we have an authenticated user
if (isset($_SESSION['authenticatedUser']) && $_SESSION["authenticatedUser"] != 'admin') {
    //If user is not admin, but accessing admin page:
    $_SESSION["message"] = "You do not have access to this page";
    header("Location: home.php"); //or wherever
} else {
    echo 'Welcome Admin!';
}
?>

 

EDIT: Fixed your code up a little.

// Check if we have an authenticated user
if (!isset($_SESSION["authenticatedUser"]) || (isset($_SESSION['authenticatedUser']) && $_SESSION['authenticatedUser'] != 'admin')) {
$_SESSION["message"] = "Please Login";
header("Location: loginpage.php");
} else {
// display page contents //
}

Might do the trick..

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.