2 Displaying DNT status with caching

While useful for testing, you probably do not want to pop up alerts when DNT is detected. You might want to show a graphic, however, to assure visitors that DNT is being honored. We do something along those lines at http://dnt.mozilla.org.

Structurally, this is very similar to detecting a DNT header and displaying an alert. The main difference is that images get cached. If you have a graphic to show DNT status, you will want to ensure you do not cache it, so that you do not display an out-of-date image.

We used one PHP file, one HTML file, and two image files. The main file:

// Save as: dnt_status.php

// dnt detection script
// Grabs the HTTP request (i.e., cookies and DNT header and referrer)
// then serves an appropriate image back.

$dnt = isset($_SERVER[‘HTTP_DNT’]) and $_SERVER[‘HTTP_DNT’] == 1;

// Force no-caching
header(“Expires: Thu, 19 Nov 1981 08:52:00 GMT”); // use any old date
header(“Cache-Control: no-store, no-cache, must-revalidate,
postcheck=0, pre-check=0”);
header(“Pragma: no-cache”);

// serve the appropriate image.
header(“Content-Type: image/png”);
if($dnt) {
} else {

This returns either the image file for users with DNT on, or the image file for users with DNT off, which we stored in images/DNT-indicator-on.png and images/DNTindicator-off.png, respectively. (You will need to create image files for your site, perhaps something as simple as a checkmark and an X while you test.)
And we invoke the dnt_status code from a third file, in our case index.html:

    <title>Do Not Track graphics</tile>  
    <img style=”border:1px solid blue;”
      alt=”Do Not Track Indicator”
      title=”Do Not Track Indicator.”/>  

Up: Tutorials

Previous: 1 Reading the DNT header

Next: 3 Collecting aggregate data based on DNT

Document Tags and Contributors

 Contributors to this page: Sheppy, trevorh, jswisher
 Last updated by: Sheppy,