pravin pravin - 5 years ago 496
PHP Question

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

I have used

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

Please give me your suggestions, how to use
variable to get rid on MSIE7/8 or is there any other alternative for

Thanks for your suggestion.


Answer Source

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) */
    // 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.

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

That way, if a user visits, they will see the referrer information if their browser has provided it, otherwise no referrer. Then when they visit, and any subsequent pages of your site, the referrer will be accessible.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download