How to prevent users from directly accessing my html website?

Viewed 344

I've been messing around and trying to create an HTML website with a login page. Problem is, you can bypass the login page by typing (domain)/page.html, so I tried using this javascript script.

<script>
var x = document.referrer;    

if (x == "(domain)") {
    console.log(x);
} else {
    window.location.href = "/";
};
</script>

After adding that to my code, I ran into another problem. I go through the login page and hit submit. It redirects me to page.html like it should, but then it instantly redirects me back to the login page. Can someone help with this?

1 Answers

On client side one can easily mess with your code.You should study how to perform authentication on the server-side.

If in case you are using PHP as backend then in login.php file you should do it as follows

//after login verified
//You have to learn how to make a verification token
setcookie("remember",$ver_token,time()+45345345 ,"/",true,);
header("Location: http://www. (domain)/page.php/");

// some additional menipulation if you want

And in the page.php file you will get the cookie else redirect to the login.php file

// On the top of the login.php file 
if (isset($_COOKIE['remember'])) {
// Check in the database and do other stuff
}
else{
header("Location: http://www. (domain)/login.php/");
}

My suggestion is first learn some about backend programming and then try to do login because a hacker or attacker may easily view the page.html file because no verification on the server side. Client side can be manipulated very very easily.

Related