javascript - Updating unique views using cookies -


i have forum thread file thread.php gets topic id information.

example:

thread.php?id=781

i'm trying create unique views setup, have no idea if feasible:

thread.php:

topicid = <?php echo $_get['id']; ?>;  if ($.cookie("unique"+topicid)!=="1") {     $.cookie("unique"+topicid,1,{expires: 1000, path: '/'}); // create cookie if doesn't exist     $.post('unique.php',{id:topicid}); // update thread unique views } 

unique.php

// connection stuff  $id = mysqli_real_escape_string($conn,$_post['id']); mysqli_query($conn,"update topics set unique_views=unique_views+1 id='$id'"); 

this create new cookie each different thread. if user views 100 threads, they'll have 100 cookies stored. i'm worried if creating new cookie each thread much. okay or there better way it?

even being possible, waste of resources. should serialize data single cookie using json, example.

in javascript can encode topics array json string json.stringify:

$.cookie("cookie_name", json.stringify(topicsid_array)); 

then can data json.parse:

var topicsid_array = json.parse($.cookie("cookie_name")); 

remember can use push js method add elements array (http://www.w3schools.com/jsref/jsref_push.asp), data cookie add new id push , save stringify.

to finish, should care duplicates. if don't want them when using push, can use https://api.jquery.com/jquery.unique/ clear array or use own js function such as:

function unique(array_source) {     var array_destination = [];     var found = false;     var x, y;      (x=0; x<array_source.length; x++)     {         found = false;         (y=0; y<array_destination.length; y++)         {             if (array_source[x] === array_destination[y])             {               found = true;               break;             }         }         if (!found)         {             array_destination.push(array_source[x]);         }     }    return array_destination; }  var topicsid_array = ['aaa', 'bbb', 'ccc', 'aaa']; topicsid_array = unique(topicsid_array); 

and in end, should work:

var topicsid_array = json.parse($.cookie("cookie_name")) topicsid_array.push("your_new_id"); topicsid_array = $.unique(topicsid_array); $.cookie("cookie_name", json.stringify(topicsid_array));; 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -