Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a page only if it is referred by Own website

Tags:

html

php

Is there a way so that a particular page is opened only if it is referred from somewhere on the same website and does not opens if direct URL is entered? I want the results page of my Quiz website to work this way. Any help? Please ask for any more details or explanations needed. I would prefer the functionality in php.

like image 318
Ankur Gupta Avatar asked Dec 03 '25 12:12

Ankur Gupta


1 Answers

You could access $_SERVER["HTTP_REFERER"];. However, you certainly can't rely on it as it can be easily spoofed. The best option is to set a session variable on page A, which they will need in order to access page B. Very basic example:

page-a.php

session_start();
$_SESSION['page_a_visited'] = true;
//Display HTML and link to page-b.php, etc.

page-b.php

session_start();
if(!isset($_SESSION['page_a_visited'])){
    //Directly accessing the page without having visited Page A.
    //Or session has expired.
    exit;
}

In response to your comment. If your quiz needs to force the user to navigate from Question 1 through to Question 10:

Instead of $_SESSION['page_a_visited'] = true;, use something like $_SESSION['page_a_visited'] = $questionId; When they're on Question 2, make sure that $_SESSION['page_a_visited'] exists and that it is equal to 1. When they're on Question 3, make sure that $_SESSION['page_a_visited'] exists and that it is equal to 2. And so on.

like image 122
Wayne Whitty Avatar answered Dec 05 '25 01:12

Wayne Whitty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!