KidsPersonalTv
KidsPersonalTv

Reputation: 3

Redirect if access page without passing through another

For example I got 3 pages:

page1.html
page2.html
page3.html

How can I block people from accessing page2 without going to page1 first?
I'd like the only way to access this page it'd be by passing to page1 and clicking a link. Then from page2, I can access page3. Thanks!
If someone tries to access it, either an error or a redirect to page1.

Upvotes: 0

Views: 38

Answers (2)

InvincibleM
InvincibleM

Reputation: 519

You can use cookies to do this. I'm not the best at javascript, but here is what I found that you can modify.

So here is a step by step process you can use to get your result. This example came from w3schools

  1. You need to set a cookie on page1.html.

function setCookie(cname,cvalue,exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

  1. Now you need to get and check the cookies.

function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

function checkCookie() {
    var user=getCookie("username");
    if (user != "") {
        alert("Welcome again " + user);
    } else {
       user = prompt("Please enter your name:","");
       if (user != "" && user != null) {
           setCookie("username", user, 30);
       }
    }
}

Hopefully, this will help you. Here is a fiddle with the complete JS: https://jsfiddle.net/uv3joc2b/

Upvotes: 1

Simon
Simon

Reputation: 784

If you are using static html pages, you can add a cookie in page1.html, then read the cookie in page2.html. If it doesn't exist redirect the user to page1.html

Upvotes: 0

Related Questions