Home > Mobile >  how to restrict access to a webpage if previouse page was not a specific page
how to restrict access to a webpage if previouse page was not a specific page

Time:08-18

I have a QR Code that brings users to PAGE A that immediately forwards to PAGE B. I want Page B to check if users came from PAGE A otherwise forwards them to PAGE C (basically say you need visit PAGE A first). The end result is that Scanning the QR Code allows access to PAGE B through PAGE A but someone can't start on PAGE B without previously being on PAGE A (unless they type it in) It's not foolproof by any means, but it's a deterrent. On Page B I am using:

if (history.back!="[page a url]") {
  location.assign("[page c url]");
}

but this doesn't seem to work.

BTW I'm hacking my way through learning any of this by trying to learn what I need to do what I need to do - please assume I know very little

CodePudding user response:

By your tags, I see that you are using WIX. I'm not sure if you have access to server side there so my answer is in javascript. This isn't full proof as you can't trust anything sent from the browser.

On page A, use localStorage to set a variable visitedA to 1 then redirect to page B.

<script>
  localStorage.setItem("visitedA","1");
  location.href = "pageB.html";
</script>

On page B check if visitedA equals 0, then redirect to C.

<script>
let visitedA = localStorage.getItem("visitedA") || "0";

if(visitedA == "0"){
 location.href = "pageC.html";
}
</script>

CodePudding user response:

Using Document.referrer is a good and most straightforward solution to this problem.

https://developer.mozilla.org/en-US/docs/Web/API/Document/referrer

example:

<script>
  // did user come from page a?
  if (document.referrer.includes('page-a') {
     // yes page-a was in the referrer
  } else {
     // no, referrer was empty or did not include page a
  }
</script>
  • Related