Alternative for storing id in hidden input Normally, when editing something, say when admin is approving a comment to a product, the id of the comment is stored in &lt;input type="hidden"&gt; to be included during form submission. But with this, just a change to the id may affect other pending comment, or those that are already approved.

So instead I used password_hash to hash the comment id, and stores it with the original comment id in a session array. Then after submitting the form, I check the submitted hashed comment id to the session array, looking for a match, then getting the original comment id.

Here's how I generate the hashed comment id:

foreach ($pendingComments as $comment) {
    $commentId = $comment['Id'];
    $hashed = password_hash($commentId, PASSWORD_DEFAULT);
    $_SESSION['COMMENT_IDS'][] = ['hash'=&gt;$hashed, 'id'=&gt;$commentId];
    $customerName = $comment['GivenName'] . ' ' . $comment['Surname'];
    $message = $comment['Message'];
    $timestamp = $comment['Timestamp'];
    echo '
        &lt;tr&gt;
            &lt;td&gt;' . $customerName . '&lt;/td&gt;
            &lt;td&gt;' . $message . '&lt;/td&gt;
            &lt;td&gt;' . $timestamp . '&lt;/td&gt;
            &lt;td&gt;
                &lt;form action="" method="POST"&gt;
                    &lt;input type="hidden" value="'.$hashed.'" name="commentId"&gt;
                    &lt;button type="submit" class="btn btn-outline-success btn-sm" name="changeCommentStatus" value="1"&gt;Approve&lt;/button&gt;
                    &lt;button type="submit" class="btn btn-outline-danger btn-sm" name="changeCommentStatus" value="2"&gt;Reject&lt;/button&gt;
                &lt;/form&gt;
             &lt;/td&gt;
         &lt;/tr&gt;
    ';
}



Here's how I check for a match after submission:

if (isset($_POST['changeCommentStatus'])) {
    $hashedId = $_POST['commentId'];
    $status = $_POST['changeCommentStatus'];
    $commentId = '';
    foreach ($_SESSION['COMMENT_IDS'] as $data) {
        $hash = $data['hash'];
        $id = $data['id'];
        if ($hashedId === $hash) {
            $commentId = $id;
            break;
        }
    }
    if (empty($commentId)) {
        // this means that the $hashedId didn't matched anything, which also means that it has been altered
        // redirect back to the page and says that the id is invalid
    }
    echo $commentId;die;
}
$_SESSION['COMMENT_IDS'] = [];


As of now, it is working. But I'm not sure if there are any critical disadvantage /drawbacks or something, or if this is not recommended at all.

Note:


Some may say that an admin will not intentionally alter the source or something, but I'm planning to use this every time I need to use &lt;input type="hidden" &gt;
This may be more useful if while a user edits his comment, and somehow changed the comment id to another comment id that also belongs to the same user, instead the other comment has been edited
I can somehow prevent the action if the replaced comment id belonged to another user, preventing the current user from indirectly editing other users' comments
And of course, this is also to be used for other content that uses &lt;input type="hidden"&gt;, especially when editing, not just for comments
I would question the premise.


  But with this, just a change to the id may affect other pending comment, or those that are already approved.


I don't see a problem here.

Given the admin is allowed to approve any comment, and nobody else is authorized to access this page completely, I don't see how it's even a problem.

To put it bluntly, nobody in the world is using such a clumsy approach as it's either useless or has a completely different solution (to ensure that a user cannot access other users' records there should be an ACL system in the database, not some HTML/Session ratatouille).