6

I have a PHP / Javascript page that automatically logs a user into different systems from one log on. These are external sites and all works good except when the user hits the back button. It then redirects them right back where they came from.

I'm looking to have it redirect back to my main website and avoid getting stuck in this redirect nightmare. So I tried document.referrer, but that only seems to grab the current page I'm on and not the referred site. Am I wrong or can this not be done like this?

function myfunc () {
    var frm = document.getElementById("loggedin1");
    if(document.referrer != 'https://someurl') {
        alert(document.referrer);//Just used to see output
        frm.submit();
    }
}

window.onload = myfunc;

If I could get it to function I would add an else in there and have it going back to my website.

Thanks!

Kaspar Lee
  • 5,446
  • 4
  • 31
  • 54
Haru
  • 1,361
  • 2
  • 15
  • 40

1 Answers1

5

It sounds like you are trying to go back to a previous page, that is not within the website of the page you're on?

A few points:
1) document.referrer will only work if the person got to the current page through a link, or clicking something.... not if they were redirected through other means.
2) Due to browser security implementations, you will not be able to access the javascript history for other sites. So if you go from your site, site A, to site B for a login, you will not be able to access the site A history from site B.

If you need to take them back to the previous page they were on on your site, can you use an iframe to load the external page? that way they'll never leave your site? Or maybe a window popup?

If what you are trying to accomplish is site logins, have you looked into the available apis? Sites like facebooks have apis for allowing logging in on your site through theirs.

Cheers!

Rebecca
  • 577
  • 4
  • 11
  • Thank you for the information. Is there a way to detect where they came from if they hit the back button to get to my site? – Haru Jan 31 '12 at 21:25
  • Hmmm... can you somehow use a hidden form? Form information tends to remain in the browser even after returning though a back-button, but I'm not sure how reliable this is. Take a look at http://stackoverflow.com/questions/829046/how-do-i-detect-if-a-user-has-got-to-a-page-using-the-back-button for some ideas – Rebecca Feb 02 '12 at 22:40