What alternative methods can be used to restrict access to specific pages based on the referring URL?
One alternative method to restrict access to specific pages based on the referring URL is to check the HTTP_REFERER server variable in PHP. This variable contains the URL of the page that linked to the current page, allowing you to verify if the user is coming from an authorized source before granting access.
<?php
$allowed_referrer = 'https://example.com'; // Define the allowed referring URL
$referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; // Get the referring URL
if (strpos($referrer, $allowed_referrer) === false) {
// Redirect the user or display an error message
header('Location: unauthorized.php');
exit;
}
?>
Related questions
-
PHP substr() strrpos()
-
naming conventions methods PHP
-
PHP automatic typecasting variables