开发者

Go back to calling website

after searching (and testing) a way to offer a kind of go-back button I am asking that question here (maybe there is an easy solution).

I have a description about orienteering on my website (5 pages): http://www.uhebeisen.net/o-def/o-definition_ge.php

There are many websites from abroad having a link to this pages. Now I'd like to get their URL if a websurfer is entering my pages. Then I can place a button go-back to my navigation list that brings him back to his page from where he clicked the link to my description-pages.

I've seen solutions using javascript:history.go(-1) or $_SERVER['HTTP_REFERER'] with PHP but problem is that a websurfer can move around my pages and if finishing his reading from any page should be provided with his (calling) URL, e.g. the one of his University.

So I need to catch his URL and store it in a safe place until he decides to leave. And if he returns to the starting page while surfing on my pages his URL shouldn't be overwritten. Since I do not program - just copy&paste and try to understand what happens. Any suggestion on how this can be done is welcome.


开发者_运维问答thank you George, that one worked I wasn't aware to place the session_start at the very beginning of the file that's why I get the two warnings. While testing this function I found that the session variables were not always cleared by the browser. Especially with Firefox, it keeps the calling URL almost forever (WinXP, FF 5.x) whereas Firefox 5 on the Mac, Safari (Mac) and Camino (Mac) work as expected: after restarting the program I can test successfully with another website.

Does Firefox have different setting possibilities in regard of sessions than other browsers?


You should store $_SERVER['HTTP_REFERER'] in the user's session upon arrival. Using this method, the value won't be overritten when the user browses within your site.

session_start();
if ( !isset( $_SESSION['referrer'] ) ) {
    if ( !empty( $_SERVER['HTTP_REFERER'] ) ) { // Because not all browsers set this
        $_SESSION['referrer'] = $_SERVER['HTTP_REFERER'];
    }
}


One way to do it would be to store somewhere (perhaps in a cookie or session, which easy to do with your PHP page) the page they're coming from, but only if that page is not on your website's domain. This would require some if-statements to set the cookie/session value appropriately, but it can be done relatively easily using particular parts of the referrer variable. There is probably a more efficient way to store this, but this is one that jumps to mind right away.

EDIT: I highly recommend George's solution, much better way to do this.


Have you tried using a session?

session_start();

if( !isset($_SESSION['refer']) )
{
    $_SESSION['refer'] = $_SERVER['HTTP_REFERER'];
}

then, once your ready to make the button, set the link to $_SESSION['refer'].


In my past projects I usually stores the redirect url following this process:

  1. search for a query string parameter url (www.yoursite.com/?redirect_url=my_encoded_url)
  2. If search at point 1 doesn't return any results, then I checks for the HTTP_REFERER

In both cases, I stores that value in a SESSION variable after verified that the url belongs to my site's domain.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜