What are best practices for preventing unauthorized access to sensitive data in PHP applications, particularly when using sessions or URLs?

To prevent unauthorized access to sensitive data in PHP applications, particularly when using sessions or URLs, it is important to properly validate user permissions before allowing access to the data. This can be achieved by implementing access control checks and ensuring that sensitive data is only accessible to authorized users.

// Check user permissions before accessing sensitive data
if($_SESSION['user_role'] !== 'admin'){
    // Redirect unauthorized users to a different page
    header('Location: unauthorized.php');
    exit;
}

// Access sensitive data only if user is authorized
$sensitiveData = $_SESSION['sensitive_data'];
echo $sensitiveData;