0

I've created a website in which user can play videos and songs. Now i want to let the user know which songs or videos he/she has already played before. For this i used Persistent cookies.

  1. but if the user played too many songs then how can i store the information of every song in cookie?
  2. Should i use a new cookie every time user plays a new song/video? if Yes then how can it be done? Because if i do so then there would be a large number of cookies created. If No then whats the alternative?
  3. Is use of cookies is the right way to accomplish this goal?

I've not used cookies before so I'm confused.

crthompson
  • 15,653
  • 6
  • 58
  • 80
Anil Kumar
  • 101
  • 10
  • 2
    Why are you using cookies for this? It seems like this data should be stored on the server in a database, that way it couldn't be accidentally cleared, or lost when moving to a new computer/device etc... – Nathan Koop Apr 21 '15 at 16:55
  • I want to let the user know which songs he/she has watched as in youtube. – Anil Kumar Apr 21 '15 at 16:59
  • 2
    like @NathanKoop mentioned, you need to store last played songs in the database. but if you still want to store it on the client side, then u can use the localstorage but even that will run out after sometime. – Sushil Apr 21 '15 at 16:59
  • @AnilKumar you have not stated why you are using cookies, a better solution seems to be using a database (like SqlServer), on the request where the user selects the movie and your server returns it, then just make a small database insert to a table (UserMoviesStarted [UserId], [MovieId], other information if required like [DateTimeStarted]) – Nathan Koop Apr 21 '15 at 17:02
  • Your user should be logged in right? I don't know what authentication you're using, but if you're using the default ASP.net framework then check out things like `HttpContext.User` (https://msdn.microsoft.com/en-us/library/system.web.httpcontext.user(v=vs.110).aspx) – Nathan Koop Apr 21 '15 at 17:07
  • User is not logged-in, how will server recognize the user next time. If i was using cookies then it would be easy to recognize the person who is using the site again. – Anil Kumar Apr 21 '15 at 17:09
  • If your user isn't logged in, then you can't do it on the server, just create a new cookie/update existing cookie and append the data. An article like this should help. https://msdn.microsoft.com/en-us/library/ms178194(v=vs.140).aspx – Nathan Koop Apr 21 '15 at 17:11
  • remember, you're going to have the same limitations that I mentioned in the first comment – Nathan Koop Apr 21 '15 at 17:11
  • you wont run out of time, you will run out of space in the localstorage if the user is listening to a lot of songs. – Sushil Apr 21 '15 at 17:18

1 Answers1

1

I'll have to agree with everyone else that cookies are not the best place for this kind of information, but....if you just want too then you can use a JQuery plugin to create and keep this kind of profile information.

A third option is to use a local file, JSON or XML, as a small data store. This would be less volatile than the cookie and would not require the dependency of sql or other db.

When implementing, place the read/writes behind an API service of some kind to make the gets and sets a bit easier.

Just some options.

Example of Jquery cookie option how to save data in a cookie using jquery

Community
  • 1
  • 1
DanoLoud
  • 24
  • 7
  • how would xml will recognized the user that has not logged in – Anil Kumar Apr 21 '15 at 17:59
  • Just like in the db example you will have to have some sort of identifier that can then be looked up in the XML document. The id could be stored in the client cookie. So to lookup the data you would get the id you stored in the cookie then retrieve the data from the XML by id using XPath. The XML could look something like: popsicle ToesOperator – DanoLoud Apr 21 '15 at 18:26
  • Xpath sample [here](http://stackoverflow.com/questions/17808399/select-nodes-with-specific-attribute-children-nodes-using-xpath) – DanoLoud Apr 21 '15 at 18:31