PDA

View Full Version : How do I handle duplicate server requests?


mlse
04-10-2005, 10:54 AM
Hi.

I'm not sure whether or not this is a PHP issue or server issue, or both! Probably both. What I want to do is to prevent the server from processing repeated requests more than once.

For example:

page_X.php (or page_X.html, or whatever) submits form data to page_Y.php at the server.

page_Y.php does it's funky thing (e.g. inserting data into a MySQL database, etc) and returns its output to the client.

Yep, nothing amazing going on there ... but what if the client repeated hammers the brower refresh button like a chimpanzee and the server receives 50 million identical requests? In the above example, page_Y.php will do 50 million identical database insertions! :eek: (depending on how I've coded it)

I have written some code in page_Y.php which checks to see whether the current IP address of the client and the contents of $_REQUEST were identical to the last time a request was received, but this has meant that I have an extra bit in the database to store this information for each and every user that logs on (because it is all sessioned and the requests need to be compared independently for each user). This is all a bit over-complicated and I wonder if there is a neat, easy, simple (i.e. built-in) way of doing this! I figure I need to know about something in the browser standard that enables the browser to know when it is submitting the same request twice - and handle it appropriately.

TIA, Mike.

marek_mar
04-10-2005, 11:50 AM
You could check the time from the last request on that session.
You could check if the sent data is identical (you could compare hashes).
You could add an image with some random numbers that the user has to type in (this is usedso that programs won't flood you with requests).
You could make the users register and allow them only so many requests.

mlse
04-10-2005, 11:52 AM
Yeah, this is all pretty much what I've done! I just wondered if there was some clever built-in to do it for you. Thanks anyway!

marek_mar
04-10-2005, 12:10 PM
What more do you expect?
The option with the image will stop robots from flooding you. If some human will want to sit there for hours and flood you manually then you can make everyone register. If that human will register a new account and send new requests you can ban the IP. If he's persistant he'll use proxys or other ways (IP's) to acces your site. You would need a doctor not a script for him :) .

mlse
04-10-2005, 12:44 PM
Lol! Yeah, ok.