What are best practices for validating a Facebook session in PHP?

When validating a Facebook session in PHP, it is important to verify the access token provided by Facebook against the Facebook Graph API to ensure that the session is valid. This can be done by making a GET request to the API endpoint with the access token and checking the response for a valid status. Additionally, it is recommended to store the access token securely and refresh it periodically to maintain the session.

<?php

$access_token = "YOUR_FACEBOOK_ACCESS_TOKEN";
$app_id = "YOUR_FACEBOOK_APP_ID";
$app_secret = "YOUR_FACEBOOK_APP_SECRET";

$response = file_get_contents("https://graph.facebook.com/debug_token?input_token=$access_token&access_token=$app_id|$app_secret");
$data = json_decode($response, true);

if ($data['data']['is_valid']) {
    echo "Session is valid!";
} else {
    echo "Session is invalid!";
}