I want to add a cookie to play a flash audio file one time only. The user should only have to hear the file one time. If they return to the website, it doesn’t load again

Any help would be appreciated
This could client side JavaScript form. The simplier the better.This doesn’t need to be secure. I just don’t want people to have to hear the sound file over and over which would be annoying.

Any code example would be great!

Actually, you want to add a cookie NOT to play the file. If the cookie doesn’t exist or doesn’t have a particular key, the flash should play and the cookie should be set to prevent the file from being played next time; otherwise, the flash shouldn’t play. As long as the user doesn’t clear cookies, it’ll be fine.

In the head of your HTML, add the following code. Note that you need to add real code to chkPlay() function where indicated. Also, I allowed the cookie’s expiration date to be one year in the future, the default value for the setCookie() function – you can make it be whatever you prefer.

<script type="text/javascript">
function chkPlay() {
// if no cookie or cookie has no item with key played…
if (null == getCookie(’played’) {
// set value for key played – value is arbitrary,
// since only existence matters
setCookie(’played’, ‘true’);
/////////////////////////////////////////////
// this is where you’ll call the audio player
/////////////////////////////////////////////
}
}
;
function getCookie(key) {
// convenience reference
var c = document.cookie;
// if none, return null – exit immediately
if ( 0 == c.length ) return null;

// implicit else – test for cookie value for lang key
var c_start = c.indexOf(key + "=");
// if no value for key, return null – exit immediately
if (-1 == c_start) return null;
// implicit else – get value from cookie string
c = c.substring(c_start + (key + "=").length);
// find end of value for key
var c_end = c.indexOf( ";" );
// if no trailing semi-colon, this is last value, reset end
if ( -1 == c_end ) c_end = c.length;

// chop trailing parts, if any and return unescaped value
return unescape( c.substring( 0, c_end ) );
}
;
function setCookie( key, value, expiredays) {
// if not given, set default number of days until expiration to one year
// in the future
if (’undefined’ == typeof expiredays) expiredays = 365.25;
// get a new Date instance
var expires = new Date();
// convert requested (or default) number of days to milliseconds
expires.setTime( expires.getTime() + 1000*24*60*60 * expiredays );
// add new key/value pair to cookies for page
document.cookie = key + ‘=’ + escape(value) + ((null == expiredays)?”:
(’;expires=’ + expires.toGMTString()));
}
;
// in case other onload handling is required, add this
// to the list of handlers
if(window.addEventListener)
window.addEventListener(’load’, chkPlay, false); // non-IE
else window.attachEvent(’onload’, chkPlay); // IE
</script>

Final note on playing audio…many people find it annoying EVER to be forced to listen to audio files they didn’t request – even once. When I hit a site that auto-plays ANY media, I hit the back button or the home key to escape the page immediately, and I never revisit that page. While I’ve answered your question, I recommend that you simply offer a "play" button (or other control) and not auto-play the media even once.

3 Responses to “web browser cookies to control audio file to play only one time for user?”

  • SomeGuy says:

    You shouldn’t do that in a cookie since they can easily be defeated.
    It needs to be controlled on the server side programming by ip address.
    References :

  • thekillerkat says:

    Simply set a cookie..
    with something like
    "played=ID1,ID2…"
    you can assign ID to each flash file on your server(of-course into some database table),
    now you have two possible way to proceed.
    1. process the cookie on client side and hide the part of your page using javascript.
    (you can send the ID along with the flash in some hidden form field or into some hidden element)
    2. do not send the code to load the flash from server while creating the HTML for the requested page.

    Use of cookie is not full-proof.. User can simply delete the cookie and play the file as many time as he/she wants.. Best way is to use a server side solution(which will be more complex than imlementing cookie based solution)..
    Kk
    References :

  • richarduie says:

    Actually, you want to add a cookie NOT to play the file. If the cookie doesn’t exist or doesn’t have a particular key, the flash should play and the cookie should be set to prevent the file from being played next time; otherwise, the flash shouldn’t play. As long as the user doesn’t clear cookies, it’ll be fine.

    In the head of your HTML, add the following code. Note that you need to add real code to chkPlay() function where indicated. Also, I allowed the cookie’s expiration date to be one year in the future, the default value for the setCookie() function – you can make it be whatever you prefer.

    <script type="text/javascript">
    function chkPlay() {
    // if no cookie or cookie has no item with key played…
    if (null == getCookie(’played’) {
    // set value for key played – value is arbitrary,
    // since only existence matters
    setCookie(’played’, ‘true’);
    /////////////////////////////////////////////
    // this is where you’ll call the audio player
    /////////////////////////////////////////////
    }
    }
    ;
    function getCookie(key) {
    // convenience reference
    var c = document.cookie;
    // if none, return null – exit immediately
    if ( 0 == c.length ) return null;

    // implicit else – test for cookie value for lang key
    var c_start = c.indexOf(key + "=");
    // if no value for key, return null – exit immediately
    if (-1 == c_start) return null;
    // implicit else – get value from cookie string
    c = c.substring(c_start + (key + "=").length);
    // find end of value for key
    var c_end = c.indexOf( ";" );
    // if no trailing semi-colon, this is last value, reset end
    if ( -1 == c_end ) c_end = c.length;

    // chop trailing parts, if any and return unescaped value
    return unescape( c.substring( 0, c_end ) );
    }
    ;
    function setCookie( key, value, expiredays) {
    // if not given, set default number of days until expiration to one year
    // in the future
    if (’undefined’ == typeof expiredays) expiredays = 365.25;
    // get a new Date instance
    var expires = new Date();
    // convert requested (or default) number of days to milliseconds
    expires.setTime( expires.getTime() + 1000*24*60*60 * expiredays );
    // add new key/value pair to cookies for page
    document.cookie = key + ‘=’ + escape(value) + ((null == expiredays)?”:
    (’;expires=’ + expires.toGMTString()));
    }
    ;
    // in case other onload handling is required, add this
    // to the list of handlers
    if(window.addEventListener)
    window.addEventListener(’load’, chkPlay, false); // non-IE
    else window.attachEvent(’onload’, chkPlay); // IE
    </script>

    Final note on playing audio…many people find it annoying EVER to be forced to listen to audio files they didn’t request – even once. When I hit a site that auto-plays ANY media, I hit the back button or the home key to escape the page immediately, and I never revisit that page. While I’ve answered your question, I recommend that you simply offer a "play" button (or other control) and not auto-play the media even once.
    References :

Leave a Reply