How can PHP be used to display website traffic statistics?
To display website traffic statistics using PHP, you can utilize tools like Google Analytics or create a custom script that tracks and displays the data. By integrating Google Analytics API or creating a script that logs visitor data into a database, you can then query and display the statistics on your website using PHP.
// Example PHP code to display website traffic statistics
// Connect to your database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "website_traffic";
$conn = new mysqli($servername, $username, $password, $dbname);
// Query the database to get website traffic statistics
$sql = "SELECT COUNT(*) as total_visits FROM visitor_logs";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output the total number of visits
while($row = $result->fetch_assoc()) {
echo "Total Visits: " . $row["total_visits"];
}
} else {
echo "0 results";
}
// Close the database connection
$conn->close();