How can PHP be used to create a temporary download link that expires after a certain period?

To create a temporary download link that expires after a certain period, you can generate a unique token for the download link and store it with an expiration timestamp in a database. When a user accesses the link, you can check if the token is valid and has not expired before allowing the download.

// Generate a unique token
$token = md5(uniqid(rand(), true));

// Set the expiration time (e.g., 1 hour)
$expiration = time() + 3600; 

// Store the token and expiration time in a database
// Example: INSERT INTO download_links (token, expiration) VALUES ('$token', '$expiration');

// Create the download link with the token
$download_link = "http://example.com/download.php?token=$token";

// Redirect the user to the download link
header("Location: $download_link");
exit;