如何检测用户设置不跟踪

How to detect users setting do-not-track

我浏览了一些 Google 文章和一些 Firefox 开发者专区,发现有一个 option you can set 可以防止某些网站跟踪您的信息。

我对此进行了调查并google 搜索了开发人员,但未能找到有关如何检测用户是否已在其浏览器中设置此项的任何信息。

它是在 POST 请求中发送还是在任何类型的请求中发送?它出现在用户代理中吗?我只是想知道如何管理这个而不是存储他们的 ips 以供登录作为示例。

发送为 an HTTP header:

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

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

或者,如果您使用 PHP 7:

function dnt_enabled(): bool
{
   return (bool)$_SERVER['HTTP_DNT'] ?? false;
}
$do_not_track_requested = ! empty( $_SERVER[ 'HTTP_DNT' ] );

所有 HTTP headers 都存在于 $_SERVER 中,前缀为 HTTP_

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

Navigator.doNotTrack

如果您想使用 client-side JavaScript 来检查用户是否拥有 dnt request header set, you can use navigator.doNotTrack.

此 属性 returns dnt HTTP header 的值(即 "1""0""unspecified").

您可以通过 PHP 通过 AJAX 发布此值来访问此值。

const is_not_trackable = navigator.doNotTrack === '1';

console.log(is_not_trackable); // True if user has requested privacy via DNT
console.log(navigator.doNotTrack); // Current value of DNT header

Note: At the time of posting, navigator.doNotTrack is still considered experimental technology. Expect behavior to change in the future, and proceed with caution before using it in your application. See Browser Compatibility.