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.
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.
This function checks if the page referring to your page is in the same "directory" and server space. I used it in my own work, maybe you get some ideas if it is not applicable directly as is.
function external_referral()
{
if( empty( $_SERVER["HTTP_REFERER"] ) )
{
return true;
}
else
{
$url = parse_url( $_SERVER["HTTP_REFERER"] );
return ( $_SERVER["HTTP_HOST"] != $url["host"] || dirname( $_SERVER["REQUEST_URI"] ) != dirname( $url["path"] ) );
}
}