Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3976244
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:46:53+00:00 2026-05-20T04:46:53+00:00

I’d like to be able to throttle login attempts based on failed attempts but

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:46:54+00:00Added an answer on May 20, 2026 at 4:46 am

    I implemented a poor-man’s throttling mechanism in phunction using APC alone, this is how I use it:

    // allow 60 requests every 30 seconds
    // each request counts as 1 (expensive operations can use higher values)
    // keep track of IPs by REMOTE_ADDR (ignore others)
    
    $throttle = ph()->Throttle($ttl = 30, $exit = 60, $count = 1, $proxy = false);
    
    if ($throttle === true)
    {
        // IP exceded 30 requests in the last 60 seconds, die() here
    }
    
    else
    {
        // $throttle is a float
        // number of requests in the last 30 seconds / 30 seconds
    
        /*
         1 req / 30 = 0,033 sec
         5 req / 30 = 0,166 sec
        10 req / 30 = 0,333 sec
        15 req / 30 = 0,5   sec
        20 req / 30 = 0,666 sec
        25 req / 30 = 0,833 sec
        30 req / 30 = 1     sec
        */
    
        usleep(intval(floatval($throttle) * 1000000));
    }
    

    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:

    function systemWide($ttl = 86400, $exit = 360)
    {
        if (extension_loaded('apc') === true)
        {
            $key = array(__FUNCTION__);
    
            if (apc_exists(__FUNCTION__) !== true)
            {
                apc_store(__FUNCTION__, 0, $ttl);
            }
    
            $result = apc_inc(__FUNCTION__, 1);
    
            if ($result < $exit)
            {
                return ($result / $ttl);
            }
    
            return true;
        }
    
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.