Generate self expiring links

I have a similar request to this one: Creating a short-term passwords

I would like to send a link (preferably automatically via contact form) to the user with an expiring download link. I’m kind of lost on how I could do this in Kirby… Maybe with a guest user?

One way to achieve this would be a route that checks an expiry date and either server the download or not.

For anyone interested:

  • I created a form, where the user is required to fill in his email adress. The controller generates a random ID with uniqid() (random enough in that context, I guess)
  • This ID is written in a structure field alongside a timestamp (now + 12 hours) to be able to check if it’s expired (For reference: https://getkirby.com/docs/reference/objects/kirby/impersonate and addToStructure field method: How to Programatically Add Entries to Structure Field (Thanks, @pedroborges)
  • The ID will also be sent in form as a link to entered email adress: www.example.com/download/{id} (in my case I made a route to get rid of ugly get parameters)
  • The download page checks if the ID exists and if it’s still valid. If both is valid it sends the pdf file as an attachment. If not, it redirects to the form and throws an error.
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename=filename.pdf');
readfile($file);

Possible improvements:

  • Deny access to the real urls to the file (although guessing it can be made difficult)
  • SQLite instead of Panel Structure
  • Delete old IDs and timestamps automatically after a while

But it’s already fine for my current use case.

It ain’t much - but it’s honest work :slight_smile:

1 Like