How can PHP be used to implement DSGVO Cookie Richtlinie Akzeptieren on a website?

To implement a DSGVO Cookie Richtlinie Akzeptieren on a website using PHP, you can create a simple cookie consent banner that allows users to accept the use of cookies. When the user clicks on the "Accept" button, a PHP script can set a cookie to indicate that the user has accepted the cookie policy. This cookie can then be checked on subsequent visits to the website to determine if the user has already accepted the cookie policy.

<?php
if(isset($_POST['accept_cookies'])) {
    setcookie('cookie_accepted', 'true', time() + (86400 * 30), '/'); // Cookie valid for 30 days
    header('Location: ' . $_SERVER['REQUEST_URI']);
    exit();
}

if(!isset($_COOKIE['cookie_accepted'])) {
    echo '<div class="cookie-banner">
            <p>This website uses cookies to ensure you get the best experience on our website.</p>
            <form method="post">
                <button type="submit" name="accept_cookies">Accept</button>
            </form>
          </div>';
}
?>