Home > database >  How do I limit access to a page only through redirect from another page
How do I limit access to a page only through redirect from another page

Time:06-26

I have two web pages, index.html and home.html. I'm trying to make home.html can only be accessed from index.html so that if a user inputs www.mywbpage.home.html, they'll be redirected to an error page or to index.html..

I linked index.html to home.html with a JS button.

Tried searching all over but the PHP, JS and .htaccess codes I got didn't work...

CodePudding user response:

This can be done without server side, but you know this is a bit sketchy.

index.html

var value = "visited";    // or the date of visit!
localStorage.setItem('my_flag', "visited");

home.html

var value = localStorage.getItem('my_flag');
if (value != "visited") {
    window.location = "index.html";
}
localStorage.removeItem("my_flag");

CodePudding user response:

you can use sessionStorage to do that first on the load index page put

sessionStorage.setItem('home', 'true');

second, if you are on the home page check if

sessionStorage.getItem('home')=="true"

if it is true let him there else redirect

CodePudding user response:

not sure if this helps, but a simple approach would be:

  1. Convert index.html and home.html to index.php and home.php, in its simple form just renaming the file should be enough.

  2. In home.php, add the check for HTTP_REFERER header like below:

<?php
$prev_url = $_SERVER['HTTP_REFERER'];
if($prev_url != "/") {  /* or use fully qualified url for index */
    header("Location: /");
    exit;
}
?>
<!-- here on put the home.html content -->
  • Related