Development
How to Rate Limit a PHP Login Form

A PHP login rate limiter should make an atomic decision in shared storage before expensive authentication work. Use bounded time windows and several signals, because a browser session counter is easy to reset and an IP address can represent many legitimate users.
The goal is to limit guessing and resource consumption while preserving a usable recovery path. Permanent account lockouts can become a denial-of-service tool against the account owner.
Choose what the login rate limit measures
Useful dimensions include a network source, a normalized account identifier, an account-and-source pair, and a global authentication budget. Each catches a different pattern. An account-wide hard block lets an attacker lock out someone else, so treat it carefully and prefer progressive friction where appropriate.
Trust the client IP from a forwarding header only when the immediate peer is a configured trusted proxy. Otherwise callers can choose their own apparent address.
Do not store raw submitted email addresses in cache keys. Normalize according to your application's account lookup rules and use an HMAC with a server-managed secret when a stable pseudonymous key is useful. This does not remove the need for a retention policy.
Make the counter and expiry atomic
For Redis, a short Lua script can increment a counter and attach an expiry in the same operation. This avoids a process dying between INCR and EXPIRE and leaving an unbounded counter.
local count = redis.call('INCR', KEYS[1])
if count == 1 then
redis.call('EXPIRE', KEYS[1], ARGV[1])
end
return {count, redis.call('TTL', KEYS[1])}
The Redis INCR documentation discusses rate-limiting patterns and the expiry race. Load the script through your installed Redis client's documented API rather than assuming different PHP clients have identical method signatures.
For a fixed-window example policy, allow a bounded number of attempts per key for a short interval and reject later attempts until expiry. Values such as five attempts per minute are policy starting points, not universal recommendations. Measure real traffic, recovery behavior, and attack patterns before selecting limits.
Apply the decision consistently
Check the appropriate counters before running password verification or calling an external identity service. Count failures and attempts according to the documented policy, including unknown accounts, so the limiter does not reveal whether an identifier exists.
Return a neutral message and an appropriate status, commonly 429 for a throttled endpoint. A Retry-After value can describe when to try again without exposing account-specific state. Keep the visible behavior consistent enough to avoid easy account enumeration.
Successful login should not erase all global and source history. Otherwise an attacker with one valid account can repeatedly reset a shared network budget. Define reset behavior separately for each dimension.
Plan for storage failure and concurrency
A limiter that works on one PHP process but not across web nodes is incomplete. Use shared storage with atomic operations, bounded connection timeouts, and monitored capacity.
Decide what happens if Redis is unavailable. Failing open preserves availability but weakens abuse controls; failing closed can block every login. Make the operational tradeoff explicit and combine it with an upstream emergency control and alerts. Do not accidentally choose behavior through an uncaught exception.
Fixed windows allow bursts around the boundary. If that matters, choose a sliding-window or token-bucket design and test its actual implementation. Adding a label such as “token bucket” to a non-atomic counter does not create its guarantees.
Verify the limiter under realistic pressure
Test sequential and concurrent requests, expiry, unknown accounts, successful login, IPv6 behavior, shared office networks, forged forwarding headers, and Redis failure. Confirm that counters expire and key cardinality remains bounded under random account names.
Monitor rejection rates alongside successful logins and support reports. Never log submitted passwords. A request identifier, policy name, and pseudonymous key are enough for most investigations.
For surrounding application boundaries, see incremental PHP refactoring. Discuss a login security review when controls differ between endpoints.