MSIE 中 $_SERVER['HTTP_REFERER'] PHP 变量的替代方案

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3966059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:35:39  来源:igfitidea点击:

Alternative for $_SERVER['HTTP_REFERER'] PHP variable in MSIE

phpinternet-explorerinternet-explorer-8httprequesthttp-referer

提问by pravin

I have used $_SERVER['HTTP_REFERER']variable in my application (used PHP). On Firefox above variables is wokring. But i observed that $_SERVER['HTTP_REFERER']is not working on Microsoft Internet Explorer 8. I also got to know that HTTP_REFERERvariable doesnt work on MSIE8 and MSIE7.

$_SERVER['HTTP_REFERER']在我的应用程序中使用了变量(使用 PHP)。在 Firefox 上,上面的变量是 wokring。但我观察到它$_SERVER['HTTP_REFERER']不适用于 Microsoft Internet Explorer 8。我还了解到该HTTP_REFERER变量不适用于 MSIE8 和 MSIE7。

Please give me your suggestions, how to use $_SERVER['HTTP_REFERER']variable to get rid on MSIE7/8 or is there any other alternative for $_SERVER['HTTP_REFERER']variable

请给我您的建议,如何使用$_SERVER['HTTP_REFERER']变量摆脱 MSIE7/8 或者是否有其他$_SERVER['HTTP_REFERER']变量替代方案

Thanks for your suggestion.

谢谢你的建议。

-Pravin.

- 普拉文。

回答by Adam

If you only need to use the referrer information internally for your website (ie: between pages of your website, not externally), you can manually keep track of a user's referrer information.

如果您只需要在您的网站内部(即:在您网站的页面之间,而不是在外部)使用引荐来源信息,您可以手动跟踪用户的引荐来源信息。

// Get the full URL of the current page
function current_page_url(){
    $page_url   = 'http';
    if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'){
        $page_url .= 's';
    }
    return $page_url.'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
}

/* (Assuming session already started) */
if(isset($_SESSION['referrer'])){
    // Get existing referrer
    $referrer   = $_SESSION['referrer'];

} elseif(isset($_SERVER['HTTP_REFERER'])){
    // Use given referrer
    $referrer   = $_SERVER['HTTP_REFERER'];

} else {
    // No referrer
}

// Save current page as next page's referrer
$_SESSION['referrer']   = current_page_url();

Then, to access the referrer, just use the $referrervariable.

然后,要访问引用者,只需使用$referrer变量。

if(isset($referrer)){
    echo 'Referred from "'.$referrer.'"';
    echo '<a href="'.$referrer.'">Back</a>';
} else {
    echo 'No referrer';
}

That way, if a user visits http://www.example.com/page_1.php, they will see the referrer information if their browser has provided it, otherwise no referrer. Then when they visit http://www.example.com/page_2.php, and any subsequent pages of your site, the referrer will be accessible.

这样,如果用户访问http://www.example.com/page_1.php,如果他们的浏览器提供了推荐人信息,他们将看到推荐人信息,否则没有推荐人。然后,当他们访问http://www.example.com/page_2.php以及您网站的任何后续页面时,将可以访问引荐来源网址。

回答by raacer

Please don't use session to guess the referrer. This will lead to undesired behaviour and strange errors.

不要使用 session 来猜测引用者。这将导致不受欢迎的行为和奇怪的错误。

If you really need to know the referring page, pass it via request parameter. You can add the parameter with JS script or on the server side.

如果您确实需要知道引用页面,请通过请求参数传递它。您可以使用 JS 脚本或在服务器端添加参数。

jQuery variant for all links on the page:

页面上所有链接的 jQuery 变体:

$(document).ready(function(){
    $('a').on('click', function(e) {
        url = this.getAttribute('href');
        // Check if the page is internal.
        if (url.charAt(0) != '/') {
            return;
        }
        e.preventDefault();
        // Append referrer.
        url += url.indexOf('?') === -1 ? '?' : '&';
        url += 'referer=' + encodeURIComponent(document.URL);
        window.location = url;
    });
});