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

2

14

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_REFERER variable doesnt work on MSIE8 and 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

Thanks for your suggestion.

-Pravin.

Bereave answered 19/10, 2010 at 7:12 Comment(3)
possible duplicate of [HTTP Referrer and IE7 and IE8 ](#2460990)Imre
bytes.com/topic/php/answers/853533-http_referer-alternativeSongful
@Col : thanks for your suggestion, is any alternative for HTTP_REFERER?Bereave
V
27

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 $referrer variable.

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.

Victory answered 22/2, 2011 at 1:41 Comment(1)
Very bad approach. This will cause strange/wrong behaviour in multitab/multiwindow session and on cached page.Charlton
C
1

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

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.

jQuery variant for all links on the page:

$(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;
    });
});
Charlton answered 13/2, 2016 at 22:21 Comment(3)
May you please give example of undesired behaviour or strange error?Kind
@AndreChenier One happens when the user uses the site in multiple windows. If the user opens a page in second window then returns to the first window and clicks the Back button, it can redirect him to the page that he opened in the second window, which is probably not what he really want or can expect from your site.Charlton
Such situations also produce errors when a programmer reley on single window mode. Also cached pages may produce very similar effect.Charlton

© 2022 - 2024 — McMap. All rights reserved.