So I'm trying to implement the evercookie on a cakePHP website, but I'm getting some really, really odd results. I've just copied and pasted the code files into my app/webroot directory, and I don't see开发者_Go百科m to be getting any 404 errors, but my cookies are not saving--they're being rewritten each and every time. What's even weirder is that when I load the page it sends at least 90 GET requests to Google.com and stores 4-5 SQLite databases in Google Chrome; the evercookie website only stores one.
My code in my generated HTML page is this:
var ec = new evercookie();
// set a cookie "id" to a random 10 character string
// usage: ec.set(key, value)
ec.set("id", "vm5m172dyg");
// 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
// the 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] + "<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 most likely the correct one
Part of this code writes to my document, and here's the output (which looks fine to me):
Storage mechanism userData returned: undefined
Storage mechanism cookieData returned: d9g6mfoo4y
Storage mechanism localData returned: d9g6mfoo4y
Storage mechanism globalData returned: undefined
Storage mechanism sessionData returned: d9g6mfoo4y
Storage mechanism windowData returned: d9g6mfoo4y
Storage mechanism historyData returned: undefined
Storage mechanism pngData returned: d9g6mfoo4y
Storage mechanism etagData returned: d9g6mfoo4y
Storage mechanism cacheData returned: d9g6mfoo4y
Storage mechanism dbData returned: d9g6mfoo4y
Storage mechanism lsoData returned: d9g6mfoo4y
Storage mechanism slData returned: d9g6mfoo4y
My problem then is how do I prevent th 90+ requests that are sent to Google? I have no idea why it's doing this. If I have, say, ten users on the site at once (which is not unbelievable), that's over 900(0). And do any of you have any idea why the cookie resets itself every time I refresh the page? That's exactly what I'm trying to prevent.
Well, don't I feel stupid! It turned out that the ec.set() call at the beginning of the code was setting the cookie at the beginning of each page load. So, I tweaked some things, and, uh, it's working now. And I'm no longer sending 90 requests to Google.
// retrieve a cookie called "id" (simply)
ec.get("id", function(value) {
if(value == undefined){
// set a cookie "id" to a random 10 character string
// usage: ec.set(key, value)
ec.set("id", "<?php echo $hash ?>");
}
else
{
// do nothing
}
});
精彩评论