How can PHP be used to track user traffic on a website?

To track user traffic on a website using PHP, you can create a script that records user visits by storing information such as IP address, timestamp, and page visited in a database. This script can be included in the header or footer of each page on the website to ensure that user visits are tracked across all pages.

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

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

// Get user IP address
$user_ip = $_SERVER['REMOTE_ADDR'];

// Get current timestamp
$timestamp = date('Y-m-d H:i:s');

// Get current page URL
$page_url = $_SERVER['REQUEST_URI'];

// Insert user visit into database
$sql = "INSERT INTO user_visits (ip_address, timestamp, page_url) VALUES ('$user_ip', '$timestamp', '$page_url')";
$conn->query($sql);

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