forked from sidstamm/dnt-demo-site
/
track.php
49 lines (42 loc) · 1.46 KB
/
track.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
require_once('./config/config.php');
// tracking script
// Grabs the HTTP request (i.e., cookies and DNT header and referrer) then
// serves an appropriate image back.
$ref = $_SERVER['HTTP_REFERER'];
if(!isset($ref)) { $ref = "unknown"; }
$dnt = isset($_SERVER['HTTP_DNT']) and $_SERVER['HTTP_DNT'] == 1;
$cky = $_COOKIE[$cfg['cookiename']];
if($dnt) {
// DNT is on... stop tracking, clear cookie, and purge database
setcookie($cfg['cookiename'], 'optout', time()-10000);
//clear from DB
$dbh = new PDO($cfg['dbconnectstring'], $cfg['dbuser'], $cfg['dbpassword']);
$stmt = $dbh->prepare("DELETE FROM tracked_sessions WHERE tracking_id LIKE ?");
$stmt->bindParam(1, $cky);
$stmt->execute();
$dbh = null;
} elseif(isset($cky) and $cky == "optout") {
//opted out via cookie. Do nothing (don't log the request)
} else {
//user didn't opt out, so log the hit.
if(!isset($cky)) {
$value = base64_encode(rand(1000000000,9999999999));
setcookie('trackingcookie', $value, time()+3600000);
}
//log $cky -> ref
$dbh = new PDO($cfg['dbconnectstring'], $cfg['dbuser'], $cfg['dbpassword']);
$stmt = $dbh->prepare("INSERT INTO tracked_sessions (tracking_id, referrer) VALUES (?, ?)");
$stmt->bindParam(1, $cky);
$stmt->bindParam(2, $ref);
$stmt->execute();
$dbh = null;
}
// then, serve the appropriate image.
header("Content-Type: image/png");
if($dnt) {
readfile("images/dnt-big-on.png");
} else {
readfile("images/dnt-big-off.png");
}
?>