Join MDN and developers like you at Mozilla's View Source conference, 12-14 September in Berlin, Germany. Learn more at

1 Reading the DNT header

This article needs a technical review. How you can help.

Most programming language tutorials start off with a “Hello, World” program to confirm you have the programming environment set up correctly. With DNT, the first thing you may want to do is confirm that you are able to successfully read an incoming DNT header. This tutorial introduces two files, a PHP file we named intojs.php, which reads the DNT value, and some sample JavaScript that displays the DNT value in an alert in a Web browser.

PHP file

First, the PHP file. You can download the sample code from Mozilla, or copy and paste this straight into a file named intojs.php saved on your Web server:

// filename: intojs.php
// dnt detection script
// Grabs the HTTP DNT request, sets the window.isDntOn variable so it
// can be checked from JavaScript code.

function getDntStatus() {
  // Handy to have as a function because you may use this a lot.
  // The important logic here is: Dnt can be on (1), off (0), or
  // unset. You want to make sure you account for unset so you do
  // not de-reference a null pointer somewhere in your code.
  // returns TRUE if Dnt is on and is equal to 1,
  // returns FALSE if Dnt is unset or not equal to 1.
  return (isset($_SERVER['HTTP_DNT']) && $_SERVER['HTTP_DNT'] == 1);
// set PHP to send javascript output to the browser
header('Content-Type: application/x-javascript');

if (getDntStatus()) {
  echo 'window.isDntOn = true;';
} else {
  echo 'window.isDntOn = false;';

JavaScript file

Second, in a JavaScript file, perhaps called hello.html and also stored on your Web server, paste the following:

    <script src="intojs.php"></script>
      alert('Hello world! DNT is set to: ' + window.isDntOn);

There are only two lines of content. In the first, we call code in the intojs.php file you created above. This assumes the JavaScript file is in the same directory as intojs.php, but you could use a relative path if it is not. You could also use an absolute path to point to code on a different server, but as we explain at the start of the tutorial, we recommend against doing so.

The main content of this JavaScript file is to create an alert and display a message to see if DNT is set to TRUE or FALSE, using the value of the window.isDntOn variable you created in intojs.php.

Testing the tutorial

Once you have the PHP and JavaScript files in place, start a Web browser and visit the hello.html file. You should see an alert that accurately reports if you have DNT on or off in that browser.

Here are some browser configurations you might want to test:

Firefox 4.0 or 5.0 and later, no DNT setting.
Should return FALSE. (May require re-installing Firefox if you have already set the DNT preference.)
Firefox 4.0 or 5.0 and later, DNT off.
Should return FALSE.
Firefox 4.0 or 5.0 and later, DNT on (Preferences > Advanced for 4.0, Preferences > Privacy for 5.0 and later).
Should return TRUE.
Internet Explorer 9.0, tracking protection lists off.
Should return FALSE.
Internet Explorer 9.0, with a tracking protection list.
Should return TRUE. (To test: Visit with IE and enable an empty TPL.)

Firefox 4.0 and 5.0 and later should work identically, so there is no need to test more than one.

Up: Tutorials

Next: 2 Displaying DNT status with caching

Document Tags and Contributors

 Contributors to this page: s@llm, Sheppy, jswisher
 Last updated by: s@llm,