Tutorials for Do Not Track

Working with the Do Not Track header is fairly easy on a technical level. In these tutorials we provide a sample code to get you up and running quickly. You can download this sample code from Mozilla at http://dnt.mozilla.org.

We expect most readers to be familiar with JavaScript. You cannot use JavaScript to read the Do Not Track value straight out of the HTTP header. We provide PHP code to set a variable in PHP, and then show you how to call the PHP code from JavaScript. You could just as easily implement this in Ruby or other languages if you prefer not to use PHP, in which case the PHP code should give you a good starting point for the logic involved. REQUIRED: You must run this PHP code (or something similar) on your Web server or you will not be able to read the Do Not Track header. You may need to install PHP on your Web server if it is not already installed.

Right now, when users enable the DNT header they can only set it as always on, and cannot specify if there are any companies they want to exempt. However, in the future, that may change. SUGGESTED: Also store and run any JavaScript code on your web server. Otherwise, if your JavaScript code is on a different server from the PHP code, you run the risk of getting a DNT value from the server the JavaScript runs on, not your main server. That would test fine today, but could break in the future if DNT changes. If for some reason you must run your JavaScript on a different server, you can put more logic into the PHP code on the main server.


  • Tutorial 1: Reading the DNT header is the “Hello, World” for DNT. We show you how to read a DNT header from website visitors, and how to pop up an alert to display the value of that header.
  • Tutorial 2: Displaying DNT status with caching moves beyond a quick alert and instead displays an image that changes based on the user’s DNT settings. This is something you might use in practice to communicate with your site’s visitors. We introduce one bit of complexity: how to handle the possibility that users changed their DNT status after caching one of the images.
  • Tutorial 3: Collecting aggregate data based on DNT illustrates how you could aggregate user data, or delete cookie data and expire cookies.

Up: The Do Not Track Field Guide

Previous section: Case studies

Next section: Additional resources

Document Tags and Contributors

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