Dokumentationsmenu

Aflæsning på serversiden

Formatet på CookieHugs samtykkecookie og aflæsning på serversiden: eksempler i PHP, Node.js, Python og C#.

Brug på serversiden

Ud over localStorage sætter CookieHug en HTTP-cookie (CookieHugConsent), så du kan tjekke samtykkestatus på serveren.

PHP

if (isset($_COOKIE['CookieHugConsent'])) {
    $consent = json_decode(
        urldecode($_COOKIE['CookieHugConsent']), true
    );

    if ($consent && $consent['statistics']) {
        // Brugeren har samtykket til statistik
        echo '<script src="https://www.googletagmanager.com/gtag/js?id=G-XXXXX"></script>';
    }

    if ($consent && $consent['marketing']) {
        // Brugeren har samtykket til markedsføring
    }
} else {
    // Intet samtykke endnu — indlæs ikke sporingsscripts
}

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) {
      // Gengiv analytics-scriptet på serveren
    }
    if (consent.marketing) {
      // Gengiv markedsføringspixels på serveren
    }
  }

  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'):
            # Gengiv analytics
            pass
        if consent.get('marketing'):
            # Gengiv markedsføring
            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)
    {
        // Gengiv analytics på serveren
    }
}