开发者

Evercookie usage question

开发者 https://www.devze.com 2023-03-17 16:52 出处:网络
I am finding it difficult to get the value of cookie set by evercookie. What i want to do is simply check if a evercookie with name is set or not set. If it is not set then iwant to set it to a somen

I am finding it difficult to get the value of cookie set by evercookie.

What i want to do is simply check if a evercookie with name is set or not set. If it is not set then iwant to set it to a somenumber.

something like below: var cookie_value = get_cookie_value; if(cookie_value == notdefined) set coookie to "12345" else want to use it in php code.

Eveercoohie usage code is given below from http://samy.pl/evercookie/

var ec = new evercookie();  

// set a cookie "id" to "12345"
// usage: ec.set(key, value)
ec.set("id", "12345");

// retrieve a cookie called "id" (simply)
ec.get("id", function(value) { alert("Cookie value is " + value) });

// or use a more advanced callback function for getting our cookie
// th开发者_开发问答e cookie value is the first param
// an object containing the different storage methods
// and returned cookie values is the second parameter
function getCookie(best_candidate, all_candidates)
{
    alert("The retrieved cookie is: " + best_candidate + "\n" +
        "You can see what each storage mechanism returned " +
        "by looping through the all_candidates object.");

    for (var item in all_candidates)
        document.write("Storage mechanism " + item +
            " returned " + all_candidates[item] + " votes<br>");
}
ec.get("id", getCookie);

// we look for "candidates" based off the number of "cookies" that
// come back matching since it's possible for mismatching cookies.
// the best candidate is very-very-likely the correct one


In the example, the cookie name is 'id', so here's how you'd check it in PHP:

<?php
if (isset($_COOKIES['id'])) {
  //... cookie was set
} else {
  //... no cookie was set
  setcookie('id',$somenumber) ;
}
?>

Unfortunately, this will only set the primary HTTP cookie, not all of the rest of the evercookie elements. However, upon next refresh, the evercookie code should detect that one of its elements contains a value, and set all of the other elements to that same value.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号