I am building a 2FA system for my website.
The login sits on the root at example.com
The 2FA script sits in example.com/2FA/verify.php
The verify.php is composed of 2 parts on the same page, separated by an IF THEN ELSE statement
Part1: enter email and send token.
Part2: after clicking send, same page refreshes and user can enter his token and click verify, then is redirected to authorized main page or not.
I am rying to restrict hotlinking and direct access to the verify.php For that purpose, i have tried to put this on top of my page:
$ref1 = $_SERVER['HTTP_REFERER'];
$ref2 = $_SERVER['PHP_SELF'];
if($ref1 !== 'https://example.com/' || $ref1 !== $ref2) {
header('Location: https://example.com');
session_destroy();
}
The direct access to verify.php works well, it gets redirected to the root. Unfortunately, i get the same result when the user enters his email and clicks send. The part2 of verify.php should appear, but gets redirected to the root.
How can i modify my top php snippet so that same page origin gets recognized and the script can complete ?