Alternative for $_Server['Http_Referer'] PHP Variable in Msie

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

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.

What can I use instead of $_SERVER['HTTP_REFERER'] in PHP for consistent data regarding the referring link?

you can pass the location to go back to in the sign-out link\button. if its a link you can add it to the url, if a button a hidden form field.

<a href="/sign-out.php?back=CURRENT_URL">

form

<input type="hidden" name="back" value="CURRENT_URL">

CURRENT_URL =$_SERVER['PHP_SELF'], or what ever is appropriate for your system.

HTTP_REFERER blank, need alternative

In short: If the user don't want it, you will never know, where he comes from. However, a more "reliable" solution may be to add the referrer to the link from the origin site to yours. Something like

<a href="http://example.com/index.php?referrer=originId">Visit example.com</a>

This requires, that external sites cannot just link to your site, but always needs to add their personal id. If this is not possible there is not much you can do.

At all its possible, that someone may change this id too.

$_SERVER['HTTP_REFERER'] not working on IE

Is this a known issue?

Yes:

'HTTP_REFERER'

The address of the page (if any) which referred the
user agent to the current page. This is set by the user agent. Not all
user agents will set this, and some provide the ability to modify
HTTP_REFERER as a feature. In short, it cannot really be trusted.

Also the above differs from what you want:

Is there a different way to get the URL that the user is using? Essentially I need to know what URL the user has entered to determine
what to display on the screen.

REQUEST_URI is what you are looking for:

'REQUEST_URI'

The URI which was given in order to access this page;

Source: http://php.net/manual/en/reserved.variables.server.php

Also see: Get the full URL in PHP

Get Referrer Page without HTTP_REFERER

That's not possible.

Best what you could do is to throw in some JavaScript.

var referrer = document.referrer;

If you need this in the PHP side (and thus the requirement can't be accomplished by JS alone), then you need to let JS send it by Ajax, or add it as a hidden input field of a form, or append as a query string to internal page links. The hard part is associating the right referrer with the requested page in the server side.

PHP HTTP_REFERRER - how to detect last page?

Wouldn't it be easier to just pass a flag in your AJAX call to tell the script which type of content to display?

Edit:

So about/staff-name.php displays the content. Call it via AJAX as about/staff-name.php?FromAjax=1

Then in the about/staff-name.php file:

if (isset($_REQUEST['FromAjax']) ) {
echo $content;
} else {
include_once 'about-header.php';
echo $content;
include_once 'about-footer.php';
}

PHP creating back-link with $_SERVER['HTTP_REFERER']

An easier way might be to do something like this:

<a href="javascript:history.back()">Go back</a>

That does not rely on the browser populating the Referer header, but instead does exactly the same thing as pressing the browser "Back" button.

This may be considered better since it actually goes back in the browser history, instead of adding the previous page to the browser history in the forward direction. It acts just as you would expect the Back button to act.

Get the previous page name in PHP (not the entire URL)

This seems to work, but there might be other elegant url functions.

$url = 'http://www.mydomain.co.uk/blist.php?prodCodes=NC023-NC022-NC024-NCB33&customerID=NHFGR';

preg_match('/\/[a-z0-9]+.php/', $url, $match);

$page = array_shift($match);

echo $page;


Related Topics



Leave a reply



Submit