点击隐藏推荐人

Hide Referrer on click

本文关键字:推荐人 隐藏      更新时间:2023-09-26

当我点击网站上的链接时,我想隐藏引用者。为了更好地了解我想做什么:当有人点击我网站上的链接时,我不想让其他网站所有者知道访问者来自哪里。

我不在乎它是用PHP、HTML还是Javascript完成的。

我尝试过HTML刷新、javascript window.location、javascript弹出、PHP头重定向,但都没用。

从2015年起,这就是防止发送Referer标头的方法:

<meta name="referrer" content="no-referrer" />

只需将此添加到网页的标题部分即可。适用于链接和Ajax请求。

在HTML中,5个链接应该支持rel="noreferrer"

这里有一个防傻的方法。我在一个应用程序中使用了这个脚本,该应用程序有时会从需要保密的页面链接到第三方网站。

<?php
session_start();
/**
  Setp 1. Get the query string variable and set it in a session, then remove it from the URL.
*/
if (isset($_GET['to']) && !isset($_SESSION['to'])) {
    $_SESSION['to'] = urldecode($_GET['to']);
    header('Location: http://yoursite.com/path/to/this-script.php');// Must be THIS script
    exit();
}

/**
  Step 2. The page has now been reloaded, replacing the original referer with  what ever this script is called.
  Make sure the session variable is set and the query string has been removed, then redirect to the intended location.
*/
if (!isset($_GET['to']) && isset($_SESSION['to'])) {
    $output = '<!DOCTYPE html>
<html>
<head>
<meta name="robots" content="none">
<title>Referral Mask</title>
</head>
<body>
<h3>Redirecting...</h3>
<script>window.location.href="'.$_SESSION['to'].'"</script>
<a href="'.$_SESSION['to'].'">Here is your link</a>
</body>
</html>' . "'n";
    unset($_SESSION['to']);
    echo $output;
    exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="robots" content="none">
<title>Referral Mask</title>
</head>
<body>
<h1>Referral Mask</h1>
<p>This resource is used to change the HTTP Referral header of a link clicked from within our secure pages.</p>
</body>
</html>

该脚本同时使用PHP和JavaScript从标头中可靠地删除原始引用。

解决方案,而非解决方案:

通过tinyurl.com或类似服务生成所有此类链接。

取你想要重定向到的<url>,并对其进行原始url编码。生成一些随机字符串,比如10-15个字符(以确保其可用性),以免称之为<alias>

然后呼叫http://tinyurl.com/create.php?alias=<alias>&url=<url>

例如。http://tinyurl.com/create.php?alias=ahdiwabdoubiadasd&url=http%3A%2F%2Fwww.whatismyreferer.com%2F

现在您可以验证http://tinyurl.com/ahdiwabdoubiadasd指向www.whatismyrefer.com,其中包含referrer伪装的

更新代码:

此代码仅为概念验证。离开父页面的导航被取消,目标url被发送到iframe。iframe加载一个dara-url,该url被视为"null"原始文档。当框架接收到消息时,它会将用户重定向到具有"null"引用的目标url。由于帧的原点为空,因此无法直接向其发送消息。因此,另一个网页可能会通过自己的匿名iframe拦截消息。在生产中,您仍然应该在链接上使用rel="noreferrer",以防用户禁用了javascript,或者页面上出现javascript错误。在禁用JS的旧浏览器的情况下,referrer仍然可以被公开。此示例可能仅在网页正文之后加载,因此脚本可能不会处理页面完全加载之前的任何单击。

改进的工作流程包括生成加密密钥,将其添加到iframe中,在发送消息之前加密目标url,然后在iframe内解密。这样你就不必担心第三方的窥探。

(function($) {
  var frame = $('<iframe sandbox="allow-scripts allow-top-navigation" src="data:text/html;charset=utf-8,<scr'ipt>window.addEventListener(''message'', function(event){ if(event.origin == ''' + window.origin + ''') top.window.location = event.data; });</scr'ipt>" style="displayyyy: none !important;">').appendTo('body');
  $('a').click(function(event) {
    frame[0].contentWindow.postMessage( event.target.href, '*' );
    return false;
  });
})(jQuery);

原始帖子:

以下是我使用空白iframe的后备解决方案的尝试。我还没有开始工作,但我会分享它,以防其他人想摆弄它。从技术上讲,框架是交叉原点的,所以你不能只点击框架中的链接。我的想法是使用postMessage使框架点击自己。

https://jsfiddle.net/skibulk/0oebphet/39/

(function($){
  var frame = $('<iframe src="about:blank" style="displayyyy: none !important;">').appendTo('body');
  $('a[rel~=noreferrer]').click(function(event){
    var win = frame[0].contentWindow;
    win.$ = $;
    frame
    .contents()
    .find('body')
    .append(event.target.outerHTML)
    .append( "<scr'ipt> window.addEventListener('message', function(event){ document.append(event.data); $('a').click(); }); </scr'ipt>" );
    win.postMessage('Hi','*');
    return false;
  });
})(jQuery);

除了jimps的答案外,我还创建了一个可同时使用HTTPS和HTTP的单文件.php解决方案。它使用两个步骤(因此它将调用anonym.php两次)。首先是javascript重定向,其次是php头位置重定向。我个人需要这个来测试在管理区域内发布的url。享受

<?php
  // anonym.php
  if ($_SERVER['QUERY_STRING']) {
    if (stripos($_SERVER['QUERY_STRING'], 'anonym2=') === FALSE) {
      echo '<script>document.location.replace("anonym.php?anonym2=' .$_SERVER['QUERY_STRING']. '");</script>';
    } else {
      header('Location: ' . str_replace('anonym2=', '', $_SERVER['QUERY_STRING']));
    }
    exit();
  }
?>

的附件

您可以让所有链接通过代理重定向或链接缩短服务(例如bit.ly或goo.gl),但这可能会引起用户的不满。

你也可以(同样,这是不可取的)用触发服务器端回发的超链接替换你的超链接,并在发送请求之前以编程方式"构造"标题。

不过,在我看来,这一切都有点过头了。

我们使用内部为内部任务系统开发的简单脚本。我们也不希望传递推荐人信息!当我观看我们管理的其他网站时,我没有看到任何引用人信息在使用脚本时随请求传递,但没有脚本。

<?php
// anonym.to.php
// Redirect URLs so the referrer information is dropped. Ideally, this script would be 
// invoked by prefixing all external links like this: "/anonym.to.php?URL"
// If a query string is given, then assume it is a website
// and anonymously redirect to it.
        if ($_SERVER['QUERY_STRING'])
        {
                header('Location: '.$_SERVER['QUERY_STRING']);
                exit(0);
        }
?>