I’d like to be able to throttle login attempts based on failed attempts but I got some questions.
Should I use MySQL? (read that it could strain the DB)
Should I throttle per user and system-wide or just system-wide? (so to stop normal people from guessing passwords)
How should I calculate my threshold? (so it automatically adapts to changes/growth)
How should I retrieve this threshold? Query/calculate on every fail or store on cache?
What should I use to throttle? (read a response that sleep() could end up straining the server)
Does anybody have some sample code?
I’m quite new at this so I appreciate the help!
Thanks
I implemented a poor-man’s throttling mechanism in phunction using APC alone, this is how I use it:
I use this on my Front-Controller and pass the value to my routing method, but that’s another story.
The bottom line is that if you use APC you’re able to keep things very fast in memory and with little memory consumption because APC follows a FILO methodology. If you need way higher timeouts you may consider using something that’s not memory based though.
BTW: MySQL supports tables with the MEMORY engine.
The problem with
sleep():A typical Apache web server with PHP installed as a module will eat about 10 MB of RAM per instance, to avoid exceeding your available ram there are some Apache settings that you can configure to limit the maximum number of instances that Apache is able to start.
The problem is when you
sleep(), that instance is still active and with enough requests could end up eating all the available slots to start new servers, thus rendering your web site inaccessible until some pending requests are completed.There is no way to overcome this from PHP AFAIK, so in the end it’s up to you.
The principle is the same for system wide throttling: