Treat recovery as an authentication feature
A password reset link can grant control of an account. Its security deserves the same care as login. Do not reveal whether an email address exists, and do not use predictable tokens or permanent reset URLs.
Generate and store tokens safely
Generate a token with random_bytes(), encode it for a URL and store only a cryptographic hash in the database. Associate it with the account, creation time, expiry and used status. The raw token should be sent to the user's verified email address and never stored in logs.
$rawToken = bin2hex(random_bytes(32));
$tokenHash = hash('sha256', $rawToken);
// Store $tokenHash, user ID, expiry and unused state.
// Send $rawToken in the HTTPS reset link.
Use a unique token record and ensure that consuming it is atomic so concurrent requests cannot both use the same token.
Keep responses generic
Return the same public message whether the account exists or not. Rate-limit requests by account identifier and network source, while avoiding permanent lockout that attackers could exploit. Apply abuse monitoring and CAPTCHA only where the threat model justifies it.
Complete the reset safely
Validate the token hash, expiry and unused state. Enforce the password policy, update the password hash with password_hash(), mark the token used and invalidate other outstanding reset tokens. Regenerate or invalidate active sessions according to the application's security policy.
Protect the reset page
Use HTTPS, prevent tokens from leaking through referrer headers, avoid third-party scripts on the reset page and redirect to a clean URL after token validation where practical. Never send a password by email.
Checklist
- Use cryptographically random, single-use tokens.
- Store only token hashes.
- Expire tokens and consume them atomically.
- Return generic request responses.
- Rate-limit recovery requests.
- Invalidate sessions and old reset links appropriately.
- Never log raw tokens or passwords.