How can PHP be used to create a script for website visitor statistics?
To create a script for website visitor statistics using PHP, you can track visitor information such as IP address, browser type, and timestamp when they access your website. This data can be stored in a database for further analysis and reporting.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "visitor_stats";
$conn = new mysqli($servername, $username, $password, $dbname);
// Get visitor information
$ip_address = $_SERVER['REMOTE_ADDR'];
$browser = $_SERVER['HTTP_USER_AGENT'];
$timestamp = date('Y-m-d H:i:s');
// Insert visitor data into database
$sql = "INSERT INTO visitors (ip_address, browser, timestamp) VALUES ('$ip_address', '$browser', '$timestamp')";
$conn->query($sql);
// Close the database connection
$conn->close();
?>