Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding a cookie to remember user preferences and override the autoplay

    I would like to apologise in advance if it looks as if I am asking a question without offering part of the solution. I have been spending a lot of time searching for a solution so that I could write something myself, but I am lost and would greatly appreciate assistance.

    I am a moderator on a website. The owner of the website uses SCM music player to play music on every page on the site. The code for the player module (which links to further coding) is as follows:

    Code:
    <!-- SCM Music Player http://scmplayer.net -->
    <script type="text/javascript" src="http://scmplayer.net/script.js" 
    data-config="{'skin':'skins/black/skin.css','volume':50,'autoplay':true,'shuffle':true,'repeat':1,'placement':'top','showplaylist':false,'playlist':
    {'title':'Aldmeri','url':'http://www.youtube.com/watch?v=ukSo-fZYLHk'},
    {'title':'Jungles of Elsweyr','url':'http://www.youtube.com/watch?v=TFrD53PWx3M'},
    {'title':'Nchuand-Zel','url':'http://www.youtube.com/watch?v=aQQzq97Kazw'},
    {'title':'ESO Theme','url':'http://www.youtube.com/watch?v=WOaWH2fp0mA'},
    {'title':'Beauty of Dawn','url':'http://www.youtube.com/watch?        feature=player_embedded&v=zXdbKz5u-a4'}]}" ></script>
    <!-- SCM Music Player script end -->
    Although he wants it to autoplay when people first visit the site, we are looking for a way for a cookie (or any other way that may work) can be added so that if a user pauses the music then this preference will be remembered the next time they visit, or open a new tab to another of the pages. Of course people may then choose to press play again during their session, so I would like their most recent preference before they left the site to be remembered.

    As you can see, the autoplay: true will need to stay the same, but there are other codes that are recognised by the player, such as:

    SCM.play(); Plays the current song.

    SCM.pause(); Pause the currently playing song

    The code that I have placed above is inserted in an HTML module in an enjin website.

    I could probably write a cookie if the value was username or something to that effect as this seems a common use, but I have no idea where to even start for this application.
    I have never written a cookie and although I have been reading much about them I cannot formulate one that would make sense in this circumstance. I would want the cookie to last a year if possible.

    If anyone could write the code for me, and where it would be placed, I would be very very grateful. I never usually ask so blatantly for pure help (I always like to do the majority of work myself), but I have already read a lot on cookies and could not figure out how to apply this information to my needs.

    Thank you in advance to everyone who looks through this problem and can offer their help.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,462
    Thanks
    23
    Thanked 634 Times in 633 Posts
    I would suggest you write the cookie in JS found here JavaScript Cookies
    And read the cookie in a server side language like PHP found here PHP Cookies The reason for server side is; you can then generate the Music Player code, but suppress the playing of the music as per the users command.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That is the problem I am having, I had already read the above links but don't know how to apply what is shown there to what I need it to do. For someone who understands cookies it must seem blatantly obvious, but for me it is like a foreign language without a bilingual dictionary.

    If anyone can help me actually write it then I would very much appreciate it. I am not lazy and asking for this type of help was not my first step by any means, but I am lost.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,156
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    Quote Originally Posted by Dragonwalker View Post
    That is the problem I am having, I had already read the above links but don't know how to apply what is shown there to what I need it to do. For someone who understands cookies it must seem blatantly obvious, but for me it is like a foreign language without a bilingual dictionary.

    If anyone can help me actually write it then I would very much appreciate it. I am not lazy and asking for this type of help was not my first step by any means, but I am lost.


    Cookies are not too difficult, but if you are lost then I suggest that you do what you would do if your car or a domestic appliance broke down or you had toothache - pay a professional who understands what they are doing to fix it. You can post in the Web Projects section of this forum.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,462
    Thanks
    23
    Thanked 634 Times in 633 Posts
    This would have been easier if the code you provided for the player was correct. I did not included a check to see if the user has cookies enabled and that should be done and if not a warning to turn them on.
    This is a PHP file not an HTML
    Code:
    <?php
    if (isset($_COOKIE["player"]))
      $play = 'false';
    else
      $play = 'true';
    ?>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    <style type="text/css">
    #select{
    width: 175px;
    padding: 10px;
    border: 1px solid black;
    border-radius:15px;
    float:left;
    }
    </style>
    </head>
    <body>
    <div id="select">
    If you want the music to play under your control and not automaticly push this button
    <button style="margin-left:5px;" onclick="pause_player();" value="PUSH">Do Not Play Next Time</button>
    </div>
    
    
    <script type="text/javascript" src="http://scmplayer.net/script.js"
    data-config="{
    'skin':'skins/black/skin.css',
    'volume':50,
    'autoplay':<?php echo $play; ?>,
    'shuffle':false,
    'repeat':0,
    'placement':'bottom',
    'showplaylist':false,
    'playlist':[{
    'title':'Aldmeri',
    'url':'http://www.youtube.com/watch?v=ukSo-fZYLHk'},
    {'title':'Beauty of Dawn',
    'url':'http://www.youtube.com/watch?v=BF8ic5AQeh0'},
    {'title':'Jungles of Elsweyr','url':'http://www.youtube.com/watch?v=TFrD53PWx3M'}]}" >
    </script>
    <script type="text/javascript">
    function pause_player(){
    	SCM.pause();
    	var d = new Date();
    	d.setTime(d.getTime()+5184000000);
    	var expires = "expires="+d.toGMTString();
    	document.cookie = 'player' + "=" + 'false' + "; " + expires;
    }
    </script>
    
    </body>
    </html>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #6
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you Sunfighter, I will look into it. I pasted the code directly from the module which is in working order so did not question it. Do you think that the code itself will need to be changed in order for a cookie to work with it?

    I was working on something last night and was trying out different codes as I had, but it didn't work. With Phillip's comment, maybe I should have given the script I had, even if it was completely wrong, so someone wouldn't feel as if they were doing work without me putting any effort in (if only he knew I was up until 5:30am trying to put different codes together from different tutorials.

    I have built websites in the past and would never expect to just sit back and let people do work without even trying, and if I wanted a lot of script written then I would hire someone. Apologies if I thought that someone would find a cookie code too much work to help for free. I help out on math forums and also do volunteer graphic design for people too (and the site I am trying to fix this for is a not-for-profit site and is not even mine but I am trying to help people who are frustrated by the music).

    I will post code once I have an idea and then if someone would be so kind to check and correct it then that would be great. But if it needs a lot of correcting then I apologise in advance.

  • #7
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, I can't edit.
    Just to clarify, the site founder does not want the player to change at all in its presets, but it needs to remember if someone paused the music and then if someone plays it (this cookie would have to override the older ones.
    I was thinking of a cookie called SCMcookie, then the value would be SCM.play() and then I would set the same cookie but with a value SCM.pause() This way whichever value was used last would be the one that would override the first.
    Would this work? Obviously I would have to figure out how to format.

    I like reading tutorials and learning, but what happens is they show a basic code, but don't explain which text should be replaced, so should all fields where it say ndays be replaced with a number, or only the line with the document.cookie ( , ,ndays).

    If there was a tutorial that gave a basic code and then highlighted where exactly changes were to be made and what type of values would go there then I would be fine and would never have to ask, but they give a very precise example, or a general example without highlighting which text would be replaced and which should stay as the word namec etc.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,156
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    You are trying to learn to swim by correspondence course. Surely at 5.30am you should recognise that you are not able to do this yourself and it would make best sense to pay a professional to do the work for you.

    You are asking for no more than an hour or so of work - but that is too much to expect for free.

    Here is a simple Javascript cookie script for you to try. The cookie is set by the user clicking on the pause button. The value of the returned cookie is either "Paused" (cookie set) or false (cookie not set). That value is returned on page load, or by clicking the Read Pause Cookie button.

    If the user clicks on the Play button you should delete the cookie. onclick = "deleteCookie('PausedCookie')";

    Obviously the user must have both Javascript and cookies enabled for this to work.

    Code:
    <html>
    <head>
    </head>
    
    <body onload = "dispCookie('PausedCookie')">
    
    <input type = "button" value = 'Set Pause Cookie' onclick = "setCookie('PausedCookie','Paused', expDate)">
    <br><br>
    <input type = "button" value='Delete Pause Cookie' onclick="deleteCookie('PausedCookie')">
    <br><br>
    <input type = "button" value = 'Read Pause Cookie' onclick = "dispCookie('PausedCookie')">
    
    <script type = "text/javascript">
    
    var expDate = new Date();
    expDate.setTime(expDate.getTime()+365*24*60*60*1000); // one year ahead
    
    function setCookie(isName,isValue,dExpires) {
    document.cookie = isName + "=" + isValue + ";expires=" + dExpires.toGMTString() + "path=/";
    alert (document.cookie);  // for testing
    }
    
    function getCookie(isName){
    cookieStr = document.cookie;
    startSlice = cookieStr.indexOf(isName+"=");
    if (startSlice == -1) {return false}
    endSlice = cookieStr.indexOf(";",startSlice+1);
    if (endSlice == -1){endSlice = cookieStr.length}
    isData = cookieStr.substring(startSlice,endSlice);
    isValue = isData.substring(isData.indexOf("=")+1,isData.length);
    return isValue;
    }
    
    function dispCookie(isName) {
    nValue = getCookie(isName);   // returns the value of the cookie
    alert ("Cookie Value: " + nValue);  // for testing
    if (nValue == "Paused") {pause_player()}  // activate pause if cookie is set
    }
    
    function deleteCookie(isName){
    if (getCookie(isName)){
    document.cookie = isName + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
    }
    
    </script>
    
    </body>
    </html>
    Last edited by Philip M; 02-26-2014 at 08:57 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, I am a very determined person and learned to build two websites from scratch. I also am a self-taught professional graphic designer who put many hours in to teach myself Illustrator and Photoshop etc. and have all 5.0 ratings with repeat customers, so I say determination to do something oneself and taking time to do so is a good thing. If I gave up after spending a few days trying something then there are many things I would not have accomplished.
    During my MSc. I had a huge data set with little independent data so I took a month to teach myself mixed linear models. Again determination to learn, and willing to try again when I fail is not necessarily a bad thing.
    I just misunderstood what this forum was about.

  • #10
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In response to the script, thank you and I will try it. The other script that was kindly submitted by sunfighter was not quite working and also I don't think the site owner wanted any kind of pop-ups.
    Again, if this was my site then I would just have the autoplay off. I am only going through all this effort because the owner is not really interested in changing it but yet some people are complaining. Just call me a people pleaser

    Also, I always try to fix things myself (not cheap but I live to learn), so I fix my own car when I can, I change thermostats in the house, fixed the fridge and microwave, changed the faucet etc. etc. LOL

  • #11
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Also, my 5:30am comment may have been a bit misleading as I don't usually head to bed until about 4:30am so it was just an extra hour just to try a few extra things. I just wanted to show that I was putting effort in and not expecting people to carry me entirely.
    (I really wish I could edit posts, sorry about the excessive number).

  • #12
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It is giving a cookie=false error.

    I will work with it and then keep trying. I will not ask for more help here as people have given enough. If I can't figure it out then I will pass the issue to the site owner and have him deal with complaints from the site members, then if he decides that he wants it fixed then he figure it out. If only our original web designer was there but he left the guild and joined another.

  • #13
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    But thank you for all the help. I really DO understand why no one would want to put too much work in for free. As a freelancer, I compete with people offering free services (got a message from a client the other day whose job I had bid for, only to ask me if the other cheap offers, and even an offer to do the work for free could be genuine - how can I compete with that? He hired one of these people). I did not mean offense, just thought it was something like a simple paste and then a change of the cookie name, value and days until expiration which is what I was trying. Did not realise that it would be something more complex.

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,156
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    Quote Originally Posted by Dragonwalker View Post
    It is giving a cookie=false error.
    It works for me. false is not an error - it means the cookie is not set. The fault, dear Brutus, lies not in the stars .....

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •