What are the legal implications of using exit popups for automatic logout in PHP, particularly in Germany?

Using exit popups for automatic logout in PHP may raise legal concerns in Germany, particularly regarding data privacy and user consent. To ensure compliance, it is important to clearly inform users about the automatic logout feature and obtain their explicit consent before implementing it. Additionally, users should have the option to easily opt out of the automatic logout if they choose to do so.

<?php
// Check if user has given consent for automatic logout
if($_COOKIE['automatic_logout_consent'] == 'true'){
    // Implement automatic logout functionality here
    session_destroy();
} else {
    // Display a popup informing the user about automatic logout and asking for consent
    echo '<script>alert("This website uses automatic logout for security purposes. Click OK to continue or Cancel to stay logged in.");</script>';
}
?>