Check URL from Document.referrer

Asked

Viewed 730 times

0

I have the following situation.

  1. When the user accesses the site: www.site.com.br I check if there is the sc=2 parameter at the end of the URL, if there is he can access the site, if there is no it is redirected to www.site.com.br/system/401

  2. On this page www.site.com.br/system/401 I saved the page where the user came from:

    var ref = Document.referrer

  3. On the same page I have a form that the user inserts his email to "login". After entering the email it is redirected to the page it was before through the code below and enter the sc=2 parameter at the end of the url:

    window.location.href = ref + (/ ?.{1,}=/.test(ref) ? '&' : '?') + 'sc=2';

  4. But imagine that the user goes directly to the page: www.site.com.br/system/401 that is, this will be the page that will be saved in Document.referrer then when the person logs in he will be redirected to the same page and the user will be in infinite loop trying to log in.

How would I check if Document.referrer matches a specific page if it had another URL? EX:

var ref = document.referrer;
if (ref == 'https://www.site.com.br/Sistema/401'){ ref = 'https://www.site.com.br'; }
window.location.href = ref + (/\?.{1,}=/.test(ref) ? '&' : '?') + 'sc=2';

I tried to do it the way above, but it didn’t work where I went wrong? In case when the user logs in:

https://www.site.com.br/Sistema/401

it should be directed to:

https://www.site.com.br

But that’s not what happens, it keeps being redirected to https://www.site.com.br/Sistema/401

  • What didn’t work? It seems to me there is no problem in this code if the goal was just to change the value of the variable ref.

  • I edited the @Sam question, you even helped me with another question about this Doc.referrer

  • Already tried to enter the URL https://www.site.com.br right in the browser to see if it goes back to https://www.site.com.br/Sistema/401? From what I could tell, I should reroute to https://www.site.com.br, except that something on the server is going back to the URL of the ref or at the index of the second URL is redirecting back.

  • If I log in to https://www.site.com.br there it directs to page 401 which is where the person enters the e-mail, then it redirects again to the https://www.site.com.br page without problems.

  • What I needed is q if the first pass was https://www.site.com.br/Sistema/401 it directed to https://www.site.com.br pq otherwise the user is in infinite loop by placing his email

  • You have to store something (maybe a Session) to know when not to redirect to page 401.

  • @Sam I’ll edit the question, I think it’s not becoming clear how it works.

  • Blz, I gave a quick read on the editing and I will read again to try to understand better, but already in advance, when the user accesses the direct page, the referrer is empty, contrary to what you say here: "But imagine that the user goes directly to the page: www.site.com.br/system/401 that is, this will be the page that will be saved in the Document.referrer"

  • Then maybe it works if I put: if (!ref){ ref = 'https://www.site.com.br/'; }

Show 4 more comments

2 answers

1

var x = Document.referrer;

var rfr = document.referrer;
//mostrando o link em um alerta
alert(rfr);
//mostrando o alerta no html de uma div
$('#ssss').html("O link visitado anteriormente foi : "+ rfr);
if (rfr=="Site acessado"){
window.location.href = "Site para carregar";
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="ssss"></div>

1


It was enough to put:

 if (!ref){ ref = 'site.com.br'; }

That worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.