Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect users setting do-not-track

I was looking through some Google articles and some Firefox developer areas and found that there was an option you can set to not let some sites track your information.

I looked into this and did some google searches for Developers and couldn't manage to find any information on how to detect whether or not a user has set this in their browser.

Is it sent in a POST request or in any type of request? Does it come in the User agent? I just wanted to know how to manage this and not store their ips for login as an example.

like image 759
Jack Hales Avatar asked May 17 '16 23:05

Jack Hales


People also ask

What is Do Not Track Privacy setting?

Look for Settings on the left and select Privacy and Security. In the center of your screen, click Cookies and other site data. To turn off tracking in Chrome, toggle the setting off for Send a “Do Not Track” request with your browsing traffic.

How do I block a tracker?

On your computer, open Chrome. Settings. Cookies and other site data. Turn Send a "Do not track" request with your browsing traffic on or off.

Can I opt out of Do Not Track?

Do Not Track (DNT) is a formerly official HTTP header field, designed to allow internet users to opt-out of tracking by websites—which includes the collection of data regarding a user's activity across multiple distinct contexts, and the retention, use, or sharing of data derived from that activity outside the context ...


2 Answers

It's sent as an HTTP header:

function dnt_enabled()
{
   return (isset($_SERVER['HTTP_DNT']) && $_SERVER['HTTP_DNT'] == 1);
}

if dnt_enabled() {
    // do stuff...
}

Or, if you're using PHP 7:

function dnt_enabled(): bool
{
   return (bool)$_SERVER['HTTP_DNT'] ?? false;
}
like image 63
miken32 Avatar answered Sep 19 '22 07:09

miken32


$do_not_track_requested = ! empty( $_SERVER[ 'HTTP_DNT' ] );

All HTTP headers are present in $_SERVER, prefixed with HTTP_.

https://www.php.net/manual/en/reserved.variables.server.php#89567

like image 38
Vladimir Kornea Avatar answered Sep 21 '22 07:09

Vladimir Kornea