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
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.

Link to comment
Share on other sites

// 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..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.