What are the potential pitfalls of using sessions to track visitor statistics on a PHP website?

Storing visitor statistics in sessions can lead to inflated numbers if visitors have multiple tabs open or if they revisit the site frequently. To accurately track visitor statistics, it is recommended to use a combination of sessions and database storage for more reliable data.

// Store visitor statistics in a database table
// Create a table in your database to store visitor statistics

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Update visitor statistics in the database
$ip_address = $_SERVER['REMOTE_ADDR'];
$page_visited = $_SERVER['REQUEST_URI'];

$sql = "INSERT INTO visitor_statistics (ip_address, page_visited, visit_timestamp) VALUES ('$ip_address', '$page_visited', NOW())";
$conn->query($sql);

// Close the database connection
$conn->close();