How can PHP be used to track and log user interactions with downloadable content, such as the time of download and the user who accessed it?

To track and log user interactions with downloadable content in PHP, you can create a database table to store the download logs. Whenever a user accesses the downloadable content, you can insert a record into this table with details such as the time of download and the user who accessed it.

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

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

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

// Get the user ID and downloadable content ID
$user_id = $_SESSION['user_id'];
$content_id = $_GET['content_id'];

// Insert a record into the download log table
$sql = "INSERT INTO download_logs (user_id, content_id, download_time) VALUES ('$user_id', '$content_id', NOW())";

if ($conn->query($sql) === TRUE) {
    echo "Download logged successfully";
} else {
    echo "Error logging download: " . $conn->error;
}

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