Menu della documentazione
Lettura lato server
Formato del cookie di consenso di CookieHug e lettura lato server: esempi in PHP, Node.js, Python e C#.
Uso lato server
Oltre al localStorage, CookieHug imposta un cookie HTTP (CookieHugConsent) che ti permette di verificare lo stato del consenso sul server.
PHP
if (isset($_COOKIE['CookieHugConsent'])) {
$consent = json_decode(
urldecode($_COOKIE['CookieHugConsent']), true
);
if ($consent && $consent['statistics']) {
// L'utente ha acconsentito alle statistiche
echo '<script src="https://www.googletagmanager.com/gtag/js?id=G-XXXXX"></script>';
}
if ($consent && $consent['marketing']) {
// L'utente ha acconsentito al marketing
}
} else {
// Nessun consenso ancora — non caricare script di tracciamento
}Node.js (Express)
const cookieParser = require('cookie-parser');
app.use(cookieParser());
app.get('/', (req, res) => {
const raw = req.cookies.CookieHugConsent;
if (raw) {
const consent = JSON.parse(decodeURIComponent(raw));
if (consent.statistics) {
// Renderizza lo script di analytics lato server
}
if (consent.marketing) {
// Renderizza i pixel di marketing lato server
}
}
res.render('index');
});Python (Django)
import json
from urllib.parse import unquote
def index(request):
raw = request.COOKIES.get('CookieHugConsent')
if raw:
consent = json.loads(unquote(raw))
if consent.get('statistics'):
# Renderizza l'analytics
pass
if consent.get('marketing'):
# Renderizza il marketing
pass
return render(request, 'index.html')C# (ASP.NET)
var raw = Request.Cookies["CookieHugConsent"];
if (!string.IsNullOrEmpty(raw))
{
var consent = JsonSerializer.Deserialize<CookieHugConsent>(
Uri.UnescapeDataString(raw)
);
if (consent.Statistics)
{
// Renderizza l'analytics lato server
}
}