What are the potential security risks associated with relying on HTTP_REFERER for user access control?

Relying on HTTP_REFERER for user access control can be risky as it can be easily spoofed or manipulated by attackers. This can lead to unauthorized access to restricted areas of a website. To mitigate this risk, it is recommended to implement server-side validation and authentication mechanisms to verify user access rights.

// Example of implementing server-side validation for user access control
session_start();

if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true){
   // User is authenticated, allow access to restricted area
   // Your code here
} else {
   // Redirect user to login page or display an error message
   header("Location: login.php");
   exit();
}