What are some potential pitfalls of using sessions to count page views in PHP?

One potential pitfall of using sessions to count page views in PHP is that it can lead to inaccurate counts if the user has multiple tabs open or if the session expires quickly. To solve this issue, a more reliable approach would be to store the page view count in a database or a file.

// Initialize session
session_start();

// Increment page view count
if (!isset($_SESSION['page_views'])) {
    $_SESSION['page_views'] = 1;
} else {
    $_SESSION['page_views']++;
}

// Display page view count
echo "Page views: " . $_SESSION['page_views'];