Have you ever been to a website that offers downloads of stuff? And while you are waiting for the download, there are lots of ads around? Sure you have. I have too.
The last day I was downloading stuff. An idea came to my head. Was there a site which required to stay and watch the ads while you are downloading? Didn’t see one. Is that technically possible? Sure. I wanted to show how. So I made a small web app for that.
You can see the
or
the full source.
How does this app work?
- The user visits the download page.
- He gets through the bushes of ads and clicks on “Download”.
- The javascript call makes a request to the server and gets the download URL.
In the background, PHP script creates a “download is active” file with the
current modification time. - The download is made through a PHP script. The script checks if the “download
is active” file has modification time of allowed age. If the file gets older
than allowed, the download is canceled server-side. - While on this page, after clicking “Download”, there is a invisible counter
started which calls a “download still active” URL that updates the modification
time of the “download is active” file. Thus, if the user doesn’t leave the page,
the download is not canceled.
Do I recommend this or tactics of this like? No. Why?
- It is dirty.
- Other services exist that offer much better alternatives.
- Having the window open doesn’t mean any of the ads will be seen. Better use other tactics.
- Only few users will read the “YOU MUST LEAVE THE WINDOW OPEN” notice, files will get corrupted, etc.
All comments and thoughts welcome.

Once the download is started, how do you get to interrupt it via PHP? Every X bytes you run a check?
In this example I am checking every 5 seconds. The download is capped @ 4KBs, and there is a sleep() command in the middle of it so it is easy to check. It is easy to check depending on data amount sent or time spent. The main point is to use fread(), but not some full-throttle functions like readfile().
In production environments it would be harder, because:
This can be avoided by using a WebSocket.
a) each user’s speed is different, so 10 seconds could be 10 kiloytes or 10 megabytes.
b) on lower quality networks with lots of packet-loss this check can lead to cancelled downloads even it the user is still on the page.
c) this method could result a DDoS on the download host because of its nature
d) every download is per-PHP-script, this means more RAM and CPU is consumed. Well… Nowdays thats not a big problem. For Google and Twitter
Etc etc.
PS> your the first to write a comment on my blog. Hooray!
why not write something about websocket.
so, I am the second one to leave messages~~~
Good point. I’ll post up a demo app in a few days.
I read a few topics. I respect your work and added blog to favorites.