I want to write a script that can only be accessed by an administrator.
This is how I want to do it:
session_start();
if (!isset($_SESSION['user_id'])) { //not logged in
//redirect to homepage
header("Location: http://domain.com/index.php");
die();
}
if ($_SESSION['user_level'] != 1337) { //not admin
//redirect to homepage
header("Location: http://domain.com/index.php");
die();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') { //form is submitted
//validate the submitted data
//submit the query
}
//form goes here
My question is: Is there a better way of validating this (eg. should all three conditionals be nested) or is this enough?
Cheers,
n1te
If it’s not possible that persons rights will change on the fly (namely: you remove admins right), then this should be enough, although I’d build a function:
For the case you’ll use extended rights checking in future.