October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Allow Multiple Account Roles to Access a PHP Page

Allow multiple PHP account roles with a strict allow-list, and avoid the boolean logic error that blocks every single-valued role.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To let both Member and Secretary accounts access a PHP page, deny access only when the user is not logged in or their role is outside that allow-list. Comparing a role against both permitted names with || makes the denial condition true for either role.

Why the original condition blocks both roles

This condition denies access when the session is missing or the role differs from either permitted value:

As an Amazon Associate I earn from qualifying purchases.

if (
    !isset($_SESSION['account_loggedin']) ||
    $_SESSION['account_loggedin'] !== true ||
    $_SESSION['account_role'] != 'Member' ||
    $_SESSION['account_role'] != 'Secretary'
) {
    // denial branch
}

A single-valued role cannot be both Member and Secretary. If it is Member, it is not Secretary; if it is Secretary, it is not Member. So at least one of the two != tests is always true, and the OR condition enters the denial branch. The relevant discussion is in the SitePoint Forums thread.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a strict allow-list

Start the session before reading session values, then allow the request only if authentication is valid and the role matches one of the permitted names:

session_start();

$loggedIn = isset($_SESSION['account_loggedin'])
    && $_SESSION['account_loggedin'] === true;
$role = $_SESSION['account_role'] ?? '';
$allowedRoles = ['Member', 'Secretary'];

if (!$loggedIn || !in_array($role, $allowedRoles, true)) {
    header('Location: login.php');
    exit;
}

// Protected page code follows here.

The third argument to in_array() enables strict comparison, checking both value and type instead of PHP’s default loose comparison. The PHP manual for in_array() recommends strict mode unless the types of all values are known with certainty. Keep exit after the redirect; otherwise the protected page could continue running.

The equivalent explicit condition is:

if (
    !$loggedIn ||
    ($role !== 'Member' && $role !== 'Secretary')
) {
    header('Location: login.php');
    exit;
}

This denies when the role is neither permitted role. The array version is easier to extend when the allow-list grows.

Keep authentication separate from authorization

Authentication establishes whether a request belongs to a logged-in user. Authorization checks what that user may access. A session flag can help establish the former, but a role-based page check must also establish the latter.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a production page, keep a user ID in the session and load the user’s current role from trusted server-side data on each request. That way, a demotion, promotion, or ban can take effect without waiting for an old role stored in the session to expire:

session_start();

$userId = $_SESSION['user_id'] ?? null;
if (!is_int($userId) && !ctype_digit((string) $userId)) {
    header('Location: login.php');
    exit;
}

// Load the current role from the database using a parameterized query.
$currentRole = loadRoleForUser((int) $userId);
$allowedRoles = ['Member', 'Secretary'];

if (!in_array($currentRole, $allowedRoles, true)) {
    http_response_code(403);
    exit('Forbidden');
}

Implement loadRoleForUser() with a parameterized database query; the example indicates where the trusted lookup belongs rather than providing a database-specific implementation. Do not use a role supplied through $_GET, $_POST, or a hidden form field as the authorization decision. Those values are controlled by the client.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Protect the session that carries the identity

Authorization checks rely on the session identifying the right user. PHP’s session-security guidance discusses strict session mode, timestamp-based session management, and regenerating session IDs with recommended procedures. Regenerate the ID at login and after privilege changes.

Configure session cookies for the deployment: use Secure over HTTPS, HttpOnly to prevent JavaScript access, and an appropriate SameSite policy. PHP documents these controls, along with session.use_strict_mode, in its session INI settings. Cookie protections reduce session risk but do not replace the per-request role check.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.