views:

95

answers:

2

I'm writing an application in PHP which I plan to sell copies of. If you need to know, it's used to allow users to download files using expiring download links.

I would like to know whenever one of my sold applications generates a download.

What would be the best way to send a notice to my php application on my server, which simply tells it "Hey, one of your scripts has done something", and what would be the best way to keep a count of the number of "hits" my server gets of this nature? A database record, or a flat text file?

I ask because I want to display a running count of the total number of downloads on my homepage, sort of like:

"Responsible for X downloads so far!"

A pure PHP solution is idea, but I suppose an ajax call would be OK too. The simpler the better, since all I am really doing is a simple $var++, only on a larger scale, right?

Anyone care to point me in the right direction?

+2  A: 

Whether by javascript or php, you need to set a url up on your server that other scripts can call. That URL should then point to a script that increments a counter. I'd put a number in a database and increment it, or if you wanted to be more detailed, you could easily break this down by month/client etc.

If you go with calling the URL from PHP, take care to ensure that the URL doesn't block the execution - ie: if your site goes down, the script you sell doesn't sit waiting for your server to respond. You can work around this in various ways - I'd do it by registering a shutdown function.

Alternatives that don't have this problem are loading the url with javascript, or as an image (but they will both likely be slightly less accurate) - I would go with image myself, as you'll get marginally better browser support.

Also, remember that unless you compile the code with something like Zend Guard, anyone can remove the remote call and prevent your counter incrementing!

benlumley
I'm not too concerned about user details, I just need a simple count, so I suppose I can go the image route. Assuming I start getting high traffic because of this, is there any way I can make the repeated db calls cause less of an impact on my server?Also, I'm not too worried about people removing the counter, it would probably be voluntary, anyway. I might need to protect something like a license key and xml-rpc or SOAP calls to authenticate the key some sort.I'll start another question for this topic. Thanks for your help.
Andrew E.
if you get a lot of traffic .. firstly, i'd worry about that if/when it happens. Incrementing a field in a db should scale well - but you could always use something like memcacheq to queue the updates and so handle them very quickly when they come in, and process them in batch from the queue.
benlumley
+1  A: 

Yeah something url callable is what you what. SOAP is probably the easiest way to go about this.

http://php.net/soap

Benlumley has a lot of valid points regarding this solution.

Also if you want to offset computation to the users browser(making web-service calls might annoy the people who buy your app, bandwidth/CPU cost) then AJAX might be a better solution.

Shane