Authentication · Lesson 2 of 7
Login System
Verify submitted credentials and establish a fresh authenticated session.
Lesson
Login looks up the account and verifies the submitted password against its stored hash with password_verify. Use a generic failure message, rate-limit attempts, and regenerate the session ID after success. Never compare plaintext passwords or reveal account existence through different errors.
Authentication confirms identity; each protected action still needs authorization.
Further guidance
Use PHP password hashing APIs, regenerate a session identifier after authentication, and clear session state during logout. Authentication identifies the user; authorization checks whether that user may perform this action on this record. Rate-limit sensitive endpoints and keep failure messages generic.
Practice task: Create a fresh example for Login System. Predict the result before running it, then change one input and explain why the result changes. Test a normal case and one boundary or invalid case where it applies. Compare your output with the example and note one rule you would use in a real project.
Code examples
Login System
PHP
if ($user && password_verify($password, $user['password'])) {
session_regenerate_id(true);
$_SESSION['user_id'] = $user['id'];
}
Expected output
A new authenticated session after valid credentials
Key points
- Verify submitted credentials and establish a fresh authenticated session.
- Store password hashes, not plaintext passwords.
- Check authorization on every protected operation.
Your learning progress
Finish this lesson
Optional practice
PHP playground
This site does not execute learner-provided PHP code. Connect a secured PHP sandbox service before enabling code execution.
Execution is disabled. No code is sent to a server.