### Initialize and Get Evercookie
Source: https://github.com/samyk/evercookie/blob/master/index.html
Demonstrates how to initialize Evercookie and retrieve a cookie's value. The `getC` function recursively fetches the cookie, updating the UI with the found value and listing all mechanisms used.
```javascript
var val = '' + Math.floor(Math.random()\*1000);
var ec = new evercookie({
/* Options */
});
getC(0);
//setTimeout(getC, 500, 1);
function getC(dont)
{
ec.get("uid", function(best, all) {
document.getElementById('idtag').innerHTML = best;
var txt = document.getElementById('cookies');
txt.innerHTML = '';
for (var item in all)
txt.innerHTML += item + ' mechanism: ' + (val == all[item] ? '' + all[item] + '' : all[item]) + '
';
if (!dont)
getC(1);
}, dont);
}
```
--------------------------------
### Google Analytics Tracking Setup
Source: https://github.com/samyk/evercookie/blob/master/index.html
This JavaScript code sets up Google Analytics tracking for the page. It dynamically loads the Google Analytics JavaScript library.
```javascript
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); try { var pageTracker = _gat._getTracker("UA-6127617-2"); pageTracker._trackPageview(); } catch(err) {}
```
--------------------------------
### Retrieve a Cookie with Evercookie (Advanced Callback)
Source: https://github.com/samyk/evercookie/blob/master/index.html
Use the `get` method with an advanced callback function that receives both the best candidate value and an object containing values from all storage mechanisms.
```javascript
// 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 +
```
--------------------------------
### Getting Cookies
Source: https://context7.com/samyk/evercookie/llms.txt
Retrieves a cookie value by checking all storage mechanisms and using a voting system to determine the most likely correct value.
```APIDOC
## Getting Cookies
The `get()` method retrieves a cookie value by checking all storage mechanisms and using a voting system to determine the most likely correct value. An optional third parameter prevents automatic re-propagation to cleared storage mechanisms.
### Method
`ec.get(key, callback, [skip_propagation])`
### Parameters
#### Path Parameters
- **key** (string) - Required - The name of the cookie to retrieve.
- **callback** (function) - Required - A function to be called with the retrieved cookie value(s).
- **skip_propagation** (integer) - Optional - If set to 1, prevents automatic re-propagation to cleared mechanisms.
### Request Example
```javascript
var ec = new evercookie();
// Simple retrieval with callback
ec.get("user_id", function(value) {
console.log("Cookie value: " + value);
});
// Advanced retrieval with full candidate information
ec.get("user_id", function(best_candidate, all_candidates) {
console.log("Best match: " + best_candidate);
for (var mechanism in all_candidates) {
console.log(mechanism + ": " + all_candidates[mechanism]);
}
});
// Retrieve WITHOUT re-propagating to cleared mechanisms
ec.get("user_id", function(value, all) {
console.log("Current value (no restoration): " + value);
}, 1);
```
### Response
#### Success Response (Callback Function)
- **value** (string) - The retrieved cookie value.
- **best_candidate** (string) - The most likely correct cookie value determined by a voting system.
- **all_candidates** (object) - An object containing all candidate values from different storage mechanisms.
```
--------------------------------
### Retrieve a Cookie with Evercookie (Simple Callback)
Source: https://github.com/samyk/evercookie/blob/master/index.html
Use the `get` method with a simple callback function to retrieve the value of a specified cookie. The callback receives the best candidate value.
```javascript
// retrieve a cookie called "id" (simply)
**ec.get("id", function(value) { alert("Cookie value is " + value) });**
```
--------------------------------
### Get Evercookie
Source: https://context7.com/samyk/evercookie/llms.txt
Retrieve a cookie value by checking all storage mechanisms. An optional parameter can prevent automatic re-propagation to cleared mechanisms.
```javascript
var ec = new evercookie();
// Simple retrieval with callback
ec.get("user_id", function(value) {
console.log("Cookie value: " + value);
// Output: Cookie value: abc123xyz
});
// Advanced retrieval with full candidate information
ec.get("user_id", function(best_candidate, all_candidates) {
console.log("Best match: " + best_candidate);
// Inspect which storage mechanisms returned what values
for (var mechanism in all_candidates) {
console.log(mechanism + ": " + all_candidates[mechanism]);
}
// Example output:
// Best match: abc123xyz
// cookieData: abc123xyz
// localData: abc123xyz
// sessionData: abc123xyz
// windowData: abc123xyz
// dbData: abc123xyz
// idbData: abc123xyz
// etagData: abc123xyz
// cacheData: abc123xyz
// pngData: abc123xyz
// lsoData: abc123xyz
// slData: abc123xyz
});
// Retrieve WITHOUT re-propagating to cleared mechanisms
// Pass 1 as third parameter to prevent auto-restoration
ec.get("user_id", function(value, all) {
console.log("Current value (no restoration): " + value);
}, 1);
```
--------------------------------
### Configure HSTS Supercookie Backend
Source: https://context7.com/samyk/evercookie/llms.txt
Deploy this script on multiple subdomains to enable multi-bit storage via HSTS headers. Requires HTTPS to function correctly.
```php
Sets HSTS header (max-age=31536000)
// DEL request: ?DEL=1 -> Clears HSTS header (max-age=0)
// Read: Browser auto-upgrades HTTP->HTTPS if HSTS set (bit=1)
// Browser allows HTTP if HSTS not set (bit=0)
// To store value 42 (binary: 00101010):
// Set HSTS on: h1, h3, h5
// Clear HSTS on: h0, h2, h4, h6, h7
```
--------------------------------
### Initialize Evercookie
Source: https://github.com/samyk/evercookie/blob/master/index.html
Include the necessary JavaScript files and initialize the Evercookie object with base configuration options.
```javascript
****
****
****
**