如何在重定向时设置自定义Referrer值?
重定向时自定义Referrer设置失败问题
我尝试在重定向时设置自定义Referrer值,分别使用JavaScript和PHP进行了尝试,但均未成功。
使用window.document.__defineGetter__的JS代码
<script> var reff = ["https://www.stackoverflow.com", "https://www.stackexchange.com"]; var randomreff = reff[Math.floor(Math.random()*reff.length)]; delete window.document.referrer; window.document.__defineGetter__('referrer', function () { return randomreff; }); </script> <script> window.onload = function() { window.location.href = "https://www.example.com"; }; </script>
使用Object.defineProperty的JS代码
const reff = ["https://www.stackoverflow.com", "https://www.stackexchange.com"]; const randomreff = reff[Math.floor(Math.random()*reff.length)]; Object.defineProperty(document, "referrer", {get : function(){ return randomreff; }}); window.onload = function() { window.location.replace("https://www.example.com"); };
PHP代码
//header("Referrer-Policy: no-referrer"); header("Referer: http://www.stackoverflow.com"); header("Location: https://example.com", true, 301);
补充说明
我已查阅文章《How to manually set REFERER header in Javascript?》,这并非重复问题。该文章是给当前窗口设置Referrer,而我需要给重定向后的窗口/网站设置自定义Referrer。
内容的提问来源于stack exchange,提问作者Mehul Kumar
相关产品推荐
相关产品推荐

