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;
}
?>
Keywords
Related Questions
- How can PHP be used to save SQL query results into multiple CSV files based on specific values in a row?
- How can PHP be used to pass and retrieve questionnaire responses using $_POST?
- What are some common pitfalls that beginners in PHP and MySQL may encounter when trying to display dynamic content in a table?