I have written a function in PHP to check whether a user can access an order on a web application that I'm making.
It checks what account owns a given order ID and if that account isn't the current session account then the idea is to make the user go back to the home page.
Unfortunately it does not seem to be working, where any user can view any order even though the SQL query is definitely correct (I have verified this through a manual SQL query).
Is there a problem in the code that I have written?
<?php
function is_accessible($document, $account) {
global $dbh;
$sth = $dbh->prepare("SELECT account FROM orders WHERE order_id = $document");
$sth->execute();
$result = $sth->fetchAll();
if ($result[0]['0'] == $account) {
return true;
}
else {
return false;
}
}
?>
<?php
if (!is_accessible($_GET['id'], $_SESSION['account'])) {
header("Location: /index.php");
}
?>
<?php
echo $_GET['id'];
10001
echo $_SESSION['account'];
1
?>