I'm trying to design a basic website for use internally...
In the database I have a user table and that table consists of "id, username, password, email, admin" The value of admin can be either 0 or 1
I want to validate whether the logged in user is an admin and then choose what to display to them based on that result.
I have tried quite a few different suggestions from multiple sites including here... The most recent code I've tried using was
if (!isset($_SESSION['id']) || !in_array($_SESSION['id'], array('1'))) {
I will post the full code below and explain what I was hoping to achieve as the outcome
<?php
session_start();
// check to see if the user is logged in
if ($_SESSION['loggedin']){
// user is logged in
?><div id="container"><?php
echo 'Welcome ' . $_SESSION['name'] . '!' . '|' . '<a href="logout.php">logout?</a></div>';
?>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<img src="images/logo.gif" class="bg">
<?php if (!isset($_SESSION['admin']) || !in_array($_SESSION['admin'], array('1'))) {
?>
<div id="container">
<div id="box">
<h2>: Add User :</h2>
<p>
<form action="action_adduser.php" method="post">
Username: <input type="username" id="newuser" name="username"> <br />
Password: <input type="password" id="newpassword" name="password"><br />
Email: <input type="email" id="email" name="email"><br />
<input type="submit" value="Add!">
</form>
</p>
</div>
</div>
<?php
}
}elseif ($_SESSION['loggedin']) {
# User is Logged in without any special permissions.
}else{
// user is not logged in, send the user to the login page
header('Location: login.php');
}
?>
I expected to (ONCE LOGGED IN) see a form to add a new user to the database if my account had the admin column set to '1'. Otherwise I expected to not see the add user form or be able to add a new user.
I can confirm when echoing out $_SESSION['admin'] it does echo out whatever that field is set to i,e 1 or 0 so the code is wrong in the validation part I'm guessing?
I wanted to do it at the start where it checks if the $_SESSION['logged_in'] is set by maybe doing something like && $_SESSION['admin'] but I don't know how to actually verify if its 1 or 0 as apposed to the code then just checking if its set at all?
Hope this makes sense and appreciate any help towards my question :)
Auth.php
<?php
session_start();
// Change this to your connection info.
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = 'withheld';
$DB_NAME = 'withheld';
// Try and connect using the info above.
$con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if ( mysqli_connect_errno() ) {
// If there is an error with the connection, stop the script and display the error.
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
// Now we check if the data was submitted, isset will check if the data exists.
if ( !isset($_POST['username'], $_POST['password']) ) {
// Could not get the data that should have been sent.
die ('Username and/or password does not exist!');
}
// Prepare our SQL
if ($stmt = $con->prepare('SELECT id, password, admin FROM accounts WHERE username = ?')) {
// Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
$stmt->bind_param('s', $_POST['username']);
$stmt->execute();
$stmt->store_result();
// Store the result so we can check if the account exists in the database.
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $password, $admin);
$stmt->fetch();
// Account exists, now we verify the password.
if (password_verify($_POST['password'], $password)) {
// Verification success! User has loggedin!
$_SESSION['loggedin'] = TRUE;
$_SESSION['name'] = $_POST['username'];
$_SESSION['admin'] = $admin;
$_SESSION['id'] = $id;
echo 'Welcome ' . $_SESSION['name'] . '!';
header('Location: index.php');
} else {
echo 'Incorrect username and/or password!';
}
} else {
echo 'Incorrect username and/or password!';
}
$stmt->close();
} else {
echo 'Could not prepare statement!';
}
?>