What are some potential pitfalls of using if statements to check the referer in PHP?

Using if statements to check the referer in PHP can be unreliable as the referer header can be easily spoofed or modified by the client. To mitigate this issue, it is recommended to use a more secure method of validating the referer, such as using a CSRF token.

// Check CSRF token instead of referer
if ($_POST['csrf_token'] === $_SESSION['csrf_token']) {
    // Proceed with the action
} else {
    // Handle CSRF token validation failure
}