Search code examples
phpdo-not-track

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.


Solution

  • 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);
    }