What is the recommended approach for determining if a page was accessed through Facebook or natively in PHP?

To determine if a page was accessed through Facebook or natively in PHP, you can check the HTTP referer header. If the referer contains "facebook.com", then the page was likely accessed through Facebook. You can use this information to customize the content or behavior of your page based on the referral source.

if(isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'facebook.com') !== false) {
    // Page was accessed through Facebook
    // Custom logic for Facebook referrals
} else {
    // Page was accessed natively
    // Custom logic for direct access
}