0
I have the following situation.
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
On this page www.site.com.br/system/401 I saved the page where the user came from:
var ref = Document.referrer
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';
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:
it should be directed to:
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
.– Sam
I edited the @Sam question, you even helped me with another question about this Doc.referrer
– Frederico Moreira
Already tried to enter the URL
https://www.site.com.br
right in the browser to see if it goes back tohttps://www.site.com.br/Sistema/401
? From what I could tell, I should reroute tohttps://www.site.com.br
, except that something on the server is going back to the URL of theref
or at the index of the second URL is redirecting back.– Sam
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.
– Frederico Moreira
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
– Frederico Moreira
You have to store something (maybe a Session) to know when not to redirect to page 401.
– Sam
@Sam I’ll edit the question, I think it’s not becoming clear how it works.
– Frederico Moreira
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"
– Sam
Then maybe it works if I put: if (!ref){ ref = 'https://www.site.com.br/'; }
– Frederico Moreira