php – How to manage audio autoplay?

Question:

My site uses an audio tag with autoplay . How to make autoplay work only on the initial page load, and when switching back from the internal one or on reloading the audio does not repeat?

Answer:

<?php
    if ($_COOKIE['audio']){
        echo '<audio src="URL"></audio>';
    }else{
        setcookie('audio',1);
        echo '<audio src="URL" autoplay=""></audio>';
    }
?>
Scroll to Top