How can one implement an opt-out option for cookies in PHP to allow users to refuse the use of functional cookies?

To implement an opt-out option for cookies in PHP to allow users to refuse the use of functional cookies, you can set a flag in a session variable when the user opts out. Then, you can check this flag before setting any functional cookies to honor the user's preference.

<?php
session_start();

if(isset($_POST['opt_out'])){
    $_SESSION['opt_out'] = true;
}

if(!isset($_SESSION['opt_out']) || $_SESSION['opt_out'] !== true){
    // Set functional cookies here
}
?>