Question

I am looking into sending the user back two (or maybe more) pages from where they came. What works so far is

<strong>
    <a href="javascript:history.go(-3)">
        Click here to go back to the view
    </a>
</strong>

However, the [history] page being called does not refresh to show the changes.

So I have the idea of referring the referrer. PHP gives me $_SERVER['REFERER'] to use, which is OK but only up to one level. How do I get the referrer (of referrer...) of $_SERVER['REFERER'] ?

Était-ce utile?

La solution 2

Pure JS. No PHP.

Modify your existing code like this.

  <strong>
        <a href="javascript:window.name='autoLoad';history.go(-3)">
            Click here to go back to the view
        </a>
    </code>

Now add this JS to the page, where you are being redirected. say test.html when you click history.go(-3)

function autoLoad(){
  if (window.name=='autoLoad') {
     location.reload();
     window.name='';
  }
}

and in the body tag add this

<body onload="autoLoad()">

Autres conseils

As far as I know, you cannot do that in PHP. The simple answer is no, because PHP is server-side, and gets just the current referrer, while Javascript is client-size, running on the browser, which actually does have 2 or more history steps.

Consider re-thinking why you want this to happen. You can never guarantee that the 2-step back referrer (even the last one) is still in your site.

In server variable $_SERVER['REFERER'] is only the last referrer. If you want to use previous you have to save them to session. The problem is that you can't find out which browser tab initialized the request - so your history would be mixed for all tabs.

Solution would be using JS to force the browser to reload page.

You could do something like this, or use cookies if you like them more, but you don`t need PHP for this.

<input type="hidden" id="reload" value="0">

<script type="text/javascript">
  window.onload = function(){
    var el = document.getElementById("reload");

    if(el.value != "0")         
    {
      window.location.reload();
    }

    el.value="1";
  }
</script>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top